Pointers and Arrays S.Bhuvaneshwari Assistant Professor/CSE Sri Eshwar College of Engineering Coimbatore
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
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. 23456 23457 23458 23459 23460 23461 23462 ……….. a[0] a[1] a[2] Base address at which the array starts. Array Name holds this address. So a=23456 The data type is int, hence each element is allocated 2 bytes. ArrayName applied with index will retrieve the value of the array elements.
 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.
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.
 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)
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:%un", a); for(i=0;i<=2;i++) printf("Address of element %d:%u and value :%dn", i, (a+i), *(a+i)); getch(); } Output:
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 (Base Address + (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
 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.
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 } }
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:
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:
 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.
int i=10; 10 int *p = &i; 23456 int **ptr = &p; 34567 23456 23457 34567 34568 4456844567 ptr p i *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. Expression Value ptr 34567 *ptr 23456 **ptr 10
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:
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.
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)
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 10000 10001 10004 10005 10008 10009 1 3 5 10002 10003 10006 10007 10010 10011 2 4 6 a[0] a[1] a[2] 2-D Array ‘a’
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.
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]: %un", 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(); }
Output:
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
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
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]: %dn", iRowCount, iColCount, iaAr[ iRowCount][iColCount]); } } getch(); }
Output:
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]: %dn", iRowCount, iColCount, *(*(iaAr+ iRowCount)+iColCount)); } } printf("n"); for(iRowCount=0;iRowCount<3;iRowCount++) { for(iColCount=0;iColCount<2;iColCount++) { printf("Value of element iaAr[%d][%d]: %dn", iRowCount, iColCount, *(iaAr[iRowCount]+iColCount)); } } getch(); }
Output:
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

Pointers and arrays

  • 1.
    Pointers and Arrays S.Bhuvaneshwari AssistantProfessor/CSE Sri Eshwar College of Engineering Coimbatore
  • 2.
    No of similardata 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
  • 3.
    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. 23456 23457 23458 23459 23460 23461 23462 ……….. a[0] a[1] a[2] Base address at which the array starts. Array Name holds this address. So a=23456 The data type is int, hence each element is allocated 2 bytes. ArrayName applied with index will retrieve the value of the array elements.
  • 4.
     As thearrayName 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.
  • 5.
    Difference between pointerand 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.
  • 6.
     Pointer arithmeticis 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)
  • 7.
    Example program wherearrayName 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:%un", a); for(i=0;i<=2;i++) printf("Address of element %d:%u and value :%dn", i, (a+i), *(a+i)); getch(); } Output:
  • 8.
    Expression Equivalent Valuedisplayed 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 (Base Address + (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
  • 9.
     ArrayName holdsthe 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.
  • 10.
    Example program toprocess 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 } }
  • 11.
    Example program toprocess 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:
  • 12.
    Example program toprocess 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:
  • 13.
     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.
  • 14.
    int i=10; 10 int *p= &i; 23456 int **ptr = &p; 34567 23456 23457 34567 34568 4456844567 ptr p i *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. Expression Value ptr 34567 *ptr 23456 **ptr 10
  • 15.
    Example program forMultipleIndirection: 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:
  • 16.
    Types of arrays: 1-Dimensionalarray (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.
  • 17.
    Memory Allocation fora 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)
  • 18.
    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 10000 10001 10004 10005 10008 10009 1 3 5 10002 10003 10006 10007 10010 10011 2 4 6 a[0] a[1] a[2] 2-D Array ‘a’
  • 19.
    Notes:  Like 1-DArray, 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.
  • 20.
    Example that illustratesthe 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]: %un", 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(); }
  • 21.
  • 22.
    Expression Equivalent Valuedisplayed 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
  • 23.
    In simple way, arrayName[row][column] Eachrow (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
  • 24.
    Example to display2-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]: %dn", iRowCount, iColCount, iaAr[ iRowCount][iColCount]); } } getch(); }
  • 25.
  • 26.
    Example to display2-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]: %dn", iRowCount, iColCount, *(*(iaAr+ iRowCount)+iColCount)); } } printf("n"); for(iRowCount=0;iRowCount<3;iRowCount++) { for(iColCount=0;iColCount<2;iColCount++) { printf("Value of element iaAr[%d][%d]: %dn", iRowCount, iColCount, *(iaAr[iRowCount]+iColCount)); } } getch(); }
  • 27.
  • 28.
    Notes:  In thesame 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