Pointers and Arrays S.Bhuvaneshwari Assistant Professor/CSE

Slides:



Advertisements
Similar presentations
UNIT IV.
Advertisements

Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
Introduction to arrays
Programming and Data Structure
Kernighan/Ritchie: Kelley/Pohl:
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
1 Lecture 9  Arrays  Declaration  Initialization  Applications  Pointers  Declaration  The & and * operators  NULL pointer  Initialization  Readings:
Introduction of Arrays. Arrays Array form an important part of almost all programming language. It provides a powerful feature and can be used as such.
Pointers Applications
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Spring 2005, Gülcihan Özdemir Dağ Lecture 7, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 7 Outline 7. 1.
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
Pointers *, &, array similarities, functions, sizeof.
© Oxford University Press All rights reserved. CHAPTER 7 POINTERS.
1. 1. Introduction to Array 2. Arrays of Data 3. Array Declaration 4. Array Initialization 5. Operations on Array 6. Multidimensional Arrays 7. Index.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
UNIT-4 1. Arrays: Definition and declaration, Initialization, Accessing elements of arrays, Storing values in arrays, Inter-function Communication: Passing.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
CS162 - Topic #12 Lecture: –Arrays with Structured Elements defining and using arrays of arrays remember pointer arithmetic Programming Project –Any questions?
Lecture 7: Arrays BJ Furman 06OCT2012. The Plan for Today Announcements Review of variables and memory Arrays  What is an array?  How do you declare.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Windows Programming Lecture 03. Pointers and Arrays.
Pointers and Arrays Subject: T0016 – ALGORITHM AND PROGRAMMING Year: 2013.
Lecture 5 Pointers 1. Variable, memory location, address, value
UMBC CMSC 104 – Section 01, Fall 2016
Chapter 8 Arrays, Strings and Pointers
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
UNIT 5 C Pointers.
Functions and Pointers
CHP-2 ARRAYS.
© 2016 Pearson Education, Ltd. All rights reserved.
Array, Strings and Vectors
EKT120 : Computer Programming
Numeric Arrays Numeric Arrays Chapter 4.
Pointers and Pointer-Based Strings
Lecture 7 – Arrays (1) PGT 106 : C PROGRAMMING.
INC 161 , CPE 100 Computer Programming
Lecture 6 C++ Programming
Functions and Pointers
Pointers in C Good morning Ladies and Gentlemen. Welcome to this talk on “Pointers in C”
Tejalal Choudhary “C Programming from Scratch” Pointers
Arrays, For loop While loop Do while loop
Chapter 5 POINTERs.
EKT150 : Computer Programming
Declaration, assignment & accessing
Lecture 18 Arrays and Pointer Arithmetic
Pointers.
Introduction To Programming Information Technology , 1’st Semester
EKT120: Computer Programming
Outline Defining and using Pointers Operations on pointers
2-d Arrays.
Introduction to Problem Solving and Programming
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
MSIS 655 Advanced Business Applications Programming
Multidimensional array
Pointer Operations.
Overloading functions
Pointers and Pointer-Based Strings
Data Structures and Algorithms Introduction to Pointers
ECE 103 Engineering Programming Chapter 38 C Pointers, Part 2
Lecture 14: Problems with Lots of Similar Data
C++ Array 1.
Introduction to Pointers
Presentation transcript:

Pointers and Arrays S.Bhuvaneshwari Assistant Professor/CSE Sri Eshwar College of Engineering Coimbatore

Arrays No of similar data items grouped together to form a single entity is called as an Array. Syntax: datatype arrayName[subscript] * Subscript or number of elements in an array. Eg: int a,b,c; can be converted as int a[2]; (i.e) a[0], a[1], a[2] a[0] Initialization: int a[2] = {1,2,3}; index

The data type is int, hence each element is allocated 2 bytes. Arrays Memory Allocation: If a[0], a[1], a[2] represent the elements of an array what does array name represent? ArrayName holds the base address at which the array starts. Base address at which the array starts. Array Name holds this address. So a=23456 23456 23457 23458 23459 23460 23461 23462 ……….. a[0] The data type is int, hence each element is allocated 2 bytes. a[1] ArrayName applied with index will retrieve the value of the array elements. a[2]

Arrays and Pointers As the arrayName holds the base address of the array, it acts as a pointer. ArrayName is a constant pointer. (i.e) it can always point to hold the same base address. Bound checking will not be done for arrays in C. Garbage value or unknown value will be returned for the Array out of range retrieval. Eg: int a[10], b[10]; a and b holds the base address of their corresponding array. Below statements are trying to change the base address ‘a’ pointing to. Hence not allowed. a=b; // illegal. a++; // illegal. a = a+2; // illegal.

Arrays and Pointers Difference between pointer and an array. Pointer ArrayName Pointer value can be changed. (i.e) Address the pointer holds. Array Name is a constant pointer. Can be made to point to any value’s address. Points to same base address always. Sizeof (pointer) will be always same. (Mostly 2 bytes). Sizeof (arrayName) varies according to the datatype. Sizeof (pointer) will give the size of only the pointer. Sizeof (arrayName) gives the size of the whole array.

Arrays and Pointers Pointer arithmetic is applicable to arrays also. Just as pointer increment or decrement, arrayName also can be incremented or decremented. Only the control will be moved. The address to which the arrayName points to cannot be changed. Pointer and arrayName are interchangeable. Pointer –Pointer is applicable to arrays. If ptr1 and ptr2 are the two pointer variables, then ptr1-ptr2= (ptr1 – ptr2) sizeof(data type)

Using arrayName as a pointer Example program where arrayName acts as a pointer. No value is assigned for element a[2], so zero will be assigned for it. void main() { int i, a[3]={1,2}; clrscr(); printf("Base Address of array:%u\n", a); for(i=0;i<=2;i++) printf("Address of element %d:%u and value :%d\n", i, (a+i), *(a+i)); getch(); } Output:

Using arrayName as a pointer Expression Equivalent Value displayed arrayName (Base Address +0) &(arrayName[0]) Base Address of the array arrayName + 1 (Base Address + (1 X sizeof(datatype)) &(arrayName[1]) Address of the second element of the array arrayName + 2 (2 X sizeof(datatype)) &(arrayName[2]) Address of the third element of the array Expression Equivalent Value displayed *arrayName (arrayName[0]) Value of the first element of the array *(arrayName + 1) (arrayName[1]) Value of the second element of the array *(arrayName + 2) (arrayName[2]) Value of the third element of the array

Using pointer for Array Processing ArrayName holds the base address of the array, hence it can be assigned to a pointer and can be used for Array processing. Auto increment or decrement can be done in the pointer variable. Array name without the brackets is the pointer name and on the other end, a pointer can be indexed as if its an array. int a[10], *b; b=a // b holds the base address of array ‘a’. a[1] will give second element of the array. b[1] will also give 2nd element of the array. Here pointer ‘b’ acts as arrayName.

Using pointer for Array Processing Example program to process array using pointer(auto increment): void main() { int iCount; /* Loop Counter */ int iaAr[5]={1,2,3,4,5}; /* Array of 5 integers */ int *piArr=iaAr; /* Pointer to array */ for(iCount=0;iCount<5;iCount++) { /* Print the content of array using the pointer */ printf("\n%d",*piArr); piArr++; // This auto increment cannot be done using arrayName }

Using pointer for Array Processing Example program to process array using pointer: void main() { int iCount; /* Loop Counter */ int iaAr[5]={1,2,3,4,5}; /* Array of 5 integers */ int *piArr=iaAr; /* Pointer to array */ for(iCount=0;iCount<5;iCount++) { /* Print the content of array using the pointer */ printf("\n %d",*(piArr+iCount)); } Output:

Using pointer for Array Processing Example program to process array using pointer: void main() { int iCount; /* Loop Counter */ int iaAr[5]={1,2,3,4,5}; /* Array of 5 integers */ int *piArr=iaAr; /* Pointer to array */ for(iCount=0;iCount<5;iCount++) { /* Print the content of array using the pointer */ printf("\n %d“, piArr[iCount]); // Indexing using pointer } Output:

Multiple Indirection Indirection (or) dereference on a pointer will retrieve the value stored in that address. A pointer can point to another pointer. The concept will be same but the application of dereference operator will vary. As a result many layers of pointer can be formed and this called multiple indirection. As such there is no limitation on the level of indirection but including great depth of indirection is difficult to follow and may lead to errors. Normally more than 2 level depth indirection is not required. A pointer to a pointer has declaration is similar to that of a normal pointer but have more asterisk sign before them indicating the depth of the pointer. Declaration: int ** pointer; The above statement creates a pointer which points to pointer to a variable with int value.

**ptr will retrieve the value 10. Multiple Indirection int i=10; 10 int *p = &i; 23456 int **ptr = &p; 34567 *ptr will retrieve the value stored in the address of ‘p’. It is also an address. To retrieve the value stored in the address ‘p’ is pointing to one more indirection is applied on *ptr. **ptr will retrieve the value 10. 23456 23457 34567 34568 ptr p i 44567 44568 Expression Value ptr 34567 *ptr 23456 **ptr 10

Using pointer for Array Processing Example program for MultipleIndirection: void main () { int i = 10; int **p1; int *p2; clrscr(); p2 = &i; p1 = &p2; /* Multiple indirection */ printf (" **p1 = %d And *p2 = %d", **p1,*p2); getch(); } Output:

Multidimensional Arrays Types of arrays: 1-Dimensional array (used in previous slides) 2-Dimensional array 3-Dimensional array and so on 1-D Array: Consists of many elements. Single row. Multiple columns. 2-D Array: Consists of many 1-D Arrays. Multiple rows and columns. 3-D Array: Consists of many 2-D Arrays.

Multidimensional Arrays Memory Allocation for a 2-D array: Even though the 2-D array is seen as rows and columns, the memory will be allocated continuously. Declaration: Datatype arrayName[rows][columns]; Rows – No Of 1-D arrays . Columns – No Of elements in each 1-D Array. Eg: int a[10], b[10], c[10]; The above three arrays can be combined together to form a single array. This forms a 2-D array with rows and columns. int a[3][10]; Here a[0], a[1], a[2] are three 1-D arrays with 10 elements in each. Sizeof 2-D Array: No of elements in 2-D Array = Rows X Columns Size of 2-D Array = No of elements X sizeof(datatype)

Multidimensional Arrays Initialization: int a[3][2]={1,2,3,4,5,6}; // Elements can be given continuously. int a[3][2]={ {1,2}, // Elements given for each 1- D Array. {3,4}, {5,6} }; int a[ ][2] = {1,2,3,4,5,6}; No Of Rows = No of Elements / No of columns 1 2 a[0] 10000 10001 10002 10003 3 4 a[1] 2-D Array ‘a’ 10004 10005 10006 10007 5 6 a[2] 10008 10009 10010 10011

Multidimensional Arrays Notes: Like 1-D Array, 2-D arrayName is also a constant pointer. In the previous example, 2-D arrayName ‘a’ is a constant pointer. It holds the base address of 2-D Array. a[0], a[1], a[2] represent 1-D Arrays. Hence they hold the base address of each 1-D array correspondingly. a[0][0], a[0][1], a[1][0], a[1][1], a[2][0], a[2][1] represent the individual elements. They hold the values.

Using arrayName as pointer in 2-D Array processing Example that illustrates the 1-d Array in each 2-D array: void main() { int iCount, iColCount; /* Loop Counter */ int iaAr[3][2]={1,2,3,4,5,6}; /* Array of 6 integers */ clrscr(); for( iCount=0;iCount<3;iCount++) for( iColCount=0;iColCount<2;iColCount++) printf("Address of element iaAr[%d][%d]: %u\n", iCount, iColCount, &(iaAr [iCount] [iColCount])); /* Address of each element*/ } for( iCount=0; iCount<3; iCount++) printf("Base Address of 1-D Array iaAr[%d]: %u \n", iCount, iaAr[iCount]); getch();

Using arrayName as pointer in 2-D Array processing Output:

Using arrayName as pointer in 2-D Array processing Expression Equivalent Value displayed arrayName (or) *arrayName (arrayName[0]) Base Address of the 2-D Array (or) Base Address of the first 1-D Array arrayName + 1 (or) *(arrayName+1) (arrayName[1]) Base Address of the second 1-D Array arrayName + 2 (or) *(arrayName+2) (arrayName[2]) Base Address of the third 1-D Array Expression Equivalent Value displayed **arrayName (arrayName[0][0]) Value of the first element of the first 1-D array **(arrayName + 1) (arrayName[1][0]) Value of the first element of the second 1-D array **(arrayName + 2) (arrayName[2][0]) Value of the first element of the third 1-D array

Using arrayName as pointer in 2-D Array processing In simple way, arrayName[row][column] Each row (1-D Array) can be retrieved using, *(arrayName + rowNumber) Each element in each row can be retrieved using, *(*(arrayName + rowNumber)+ columnNumber) Expression Equivalent Value displayed *(*arrayName+0)+1) (arrayName[0][1]) Value of the second element of the first 1-D array *(*(arrayName + 1)+1) (arrayName[1][1]) Value of the second element of the second 1-D array *(*(arrayName + 2)+1) (arrayName[2][1]) Value of the second element of the third 1-D array

Using arrayName as pointer in 2-D Array processing Example to display 2-D Array using index: void main() { int iRowCount, iColCount; /* Loop Counter */ int iaAr[3][2]={1,2,3,4,5,6}; /* Array of 6 integers */ clrscr(); for( iRowCount=0;iRowCount<3;iRowCount++) for(iColCount=0;iColCount<2;iColCount++) printf("Value of element iaAr[%d][%d]: %d\n", iRowCount, iColCount, iaAr[ iRowCount][iColCount]); } getch();

Using arrayName as pointer in 2-D Array processing Output:

Using arrayName as pointer in 2-D Array processing Example to display 2-D Array using arrayName as pointer: void main() { int iRowCount, iColCount; /* Loop Counter */ int iaAr[3][2]={1,2,3,4,5,6}; /* Array of 6 integers */ clrscr(); for(iRowCount=0;iRowCount<3;iRowCount++){ for(iColCount=0;iColCount<2;iColCount++){ printf("Value of element iaAr[%d][%d]: %d\n", iRowCount, iColCount, *(*(iaAr+ iRowCount)+iColCount)); } } printf("\n"); for(iRowCount=0;iRowCount<3;iRowCount++) { for(iColCount=0;iColCount<2;iColCount++) { *(iaAr[iRowCount]+iColCount)); getch(); }

Using arrayName as pointer in 2-D Array processing Output:

Multidimensional Arrays Notes: In the same way, 2-D array is processed, 3-D, 4-D etc., arrays can also be processed. 3-D array is formed by combining multiple 2-D Arrays. Indirection(*) first applied on the arrayName, will retrieve the 2-D array’s base address. Second indirection will retrieve the 1-D array’s base address. Third indirection will retrieve the element value. Hence according to the dimension, the number of indirection operators to be applied to retrieve the value will vary. Eg: int a[2][3][4]; To retrieve the value of element a[0][1][3] using arrayName as pointer, *(*(*(a+0)+1)+3) has to be used. No of Elements = 2X3X4 = 24 Sizeof(a) = 24X2 = 48 bytes