CSO101: Computer Programming
Pointers & Arrays
O.S.L. Bhavana
Passing Arrays via pointers: Recall
re-assigned different values but array name should not be
re-assigned to a different address.
1-D Array and Pointers
int a[3]={2,5,6};
1000 | | | | a[0] |
| 2 | | ||
| | | ||
| | |
1004 | | | | a[1] |
| 5 | | ||
| | | ||
| | |
1008 | | | | a[2] |
| 6 | | ||
| | | ||
| | |
An array name has multiple interpretations
1-D Array and Pointers
int a[3]={2,5,6};
a = 1000 | | | | a[0] |
| 2 | | ||
| | | ||
| | |
1004 | | | | a[1] |
| 5 | | ||
| | | ||
| | |
1008 | | | | a[2] |
| 6 | | ||
| | | ||
| | |
An array name has multiple interpretations
1-D Array and Pointers
int a[3]={2,5,6};
a = 1000 | | | | a[0] |
| 2 | | ||
| | | ||
| | |
1004 | | | | a[1] |
| 5 | | ||
| | | ||
| | |
1008 | | | | a[2] |
| 6 | | ||
| | | ||
| | |
An array name has multiple interpretations
(not the entire array)
a+1 = address of a + 1 (size of (a[0]))
= address of a + 4
2-D Array Memory Map
int a[3][4]={{2,5,6,7}, {12,15,16,17}, {22,25,26,27}};
1000 | 2 | a[0][0] |
1004 | 5 | a[0][1] |
1008 | 6 | a[0][2] |
1012 | 7 | a[0][3] |
1016 | 12 | a[1][0] |
1020 | 15 | a[1][1] |
1024 | 16 | a[1][2] |
1028 | 17 | a[1][3] |
1032 | 22 | a[2][0] |
1036 | 25 | a[2][1] |
1040 | 26 | a[2][2] |
1044 | 27 | a[2][1] |
For simplicity we view 2D arrays as matrices
with the left dimension specifying no. of rows
right dimension specifies no. of columns
a[0] | 2 | 5 | 6 | 7 |
a[1] | 12 | 15 | 16 | 17 |
a[2] | 22 | 25 | 26 | 27 |
2-D Array Addressing
int a[3][4]={{2,5,6,7}, {12,15,16,17}, {22,25,26,27}};
1000 | 2 | a[0][0] |
1004 | 5 | a[0][1] |
1008 | 6 | a[0][2] |
1012 | 7 | a[0][3] |
1016 | 12 | a[1][0] |
1020 | 15 | a[1][1] |
1024 | 16 | a[1][2] |
1028 | 17 | a[1][3] |
1032 | 22 | a[2][0] |
1036 | 25 | a[2][1] |
1040 | 26 | a[2][2] |
1044 | 27 | a[2][1] |
Address of a[i][j] = address of a[0][0]
+( i * num_cols +j) sizeof_datatype
= address of a[0][0] + +( i * num_cols +j) 4
;
a[0] | 2 | 5 | 6 | 7 |
a[1] | 12 | 15 | 16 | 17 |
a[2] | 22 | 25 | 26 | 27 |
3-D Array Memory Map
int a[2][3][2]={2,5,6,7,12,15,16,17,22,25,26,27};
1000 | 2 | a[0][0][0] |
1004 | 5 | a[0][0][1] |
1008 | 6 | a[0][1][0] |
1012 | 7 | a[0][1][1] |
1016 | 12 | a[0][2][0] |
1020 | 15 | a[0][2][1] |
1024 | 16 | a[1][0][0] |
1028 | 17 | a[1][0][1] |
1032 | 22 | a[1][1][0] |
1036 | 25 | a[1][1][1] |
1040 | 26 | a[1][2][0] |
1044 | 27 | a[1][2][1] |
a[0] | | |
| | |
| | |
| | |
a[1] | ||
| | |
| |
3-D Array Memory Map
int a[2][3][2]={2,5,6,7,12,15,16,17,22,25,26,27};
1000 | 2 | a[0][0][0] |
1004 | 5 | a[0][0][1] |
1008 | 6 | a[0][1][0] |
1012 | 7 | a[0][1][1] |
1016 | 12 | a[0][2][0] |
1020 | 15 | a[0][2][1] |
1024 | 16 | a[1][0][0] |
1028 | 17 | a[1][0][1] |
1032 | 22 | a[1][1][0] |
1036 | 25 | a[1][1][1] |
1040 | 26 | a[1][2][0] |
1044 | 27 | a[1][2][1] |
a[0] | a[0][0] | a[0][0][0]=2 |
a[0][0][1]=5 | ||
a[0][1] | a[0][1][0]=6 | |
a[0][1][1]=7 | ||
a[0][2] | a[0][2][0]=12 | |
a[0][2][1]=15 | ||
a[1] | a[1][0] | a[1][0][0]=16 |
a[1][0][1]=17 | ||
a[1][1] | a[1][1][0]=22 | |
a[1][1][1]=25 | ||
a[1][2] | a[1][2][0]=26 | |
a[1][2][1]=27 |
2-D Array and Pointers
Interpretation of 2-D array:
– is a collection of 3*4 elements
int a[3][4]={{2,5,6,7}, {12,15,16,17}, {22,25,26,27}};
1000 | 2 | a[0][0] |
1004 | 5 | a[0][1] |
1008 | 6 | a[0][2] |
1012 | 7 | a[0][3] |
1016 | 12 | a[1][0] |
1020 | 15 | a[1][1] |
1024 | 16 | a[1][2] |
1028 | 17 | a[1][3] |
1032 | 22 | a[2][0] |
1036 | 25 | a[2][1] |
1040 | 26 | a[2][2] |
1044 | 27 | a[2][1] |
2-D Array and Pointers
int a[3][4]={{2,5,6,7}, {12,15,16,17}, {22,25,26,27}};
a=1000 | 2 | a[0][0] |
1004 | 5 | a[0][1] |
1008 | 6 | a[0][2] |
1012 | 7 | a[0][3] |
1016 | 12 | a[1][0] |
1020 | 15 | a[1][1] |
1024 | 16 | a[1][2] |
1028 | 17 | a[1][3] |
1032 | 22 | a[2][0] |
1036 | 25 | a[2][1] |
1040 | 26 | a[2][2] |
1044 | 27 | a[2][1] |
Interpretation of 2-D array:
2-D Array and Pointers
int a[3][4]={{2,5,6,7}, {12,15,16,17}, {22,25,26,27}};
a=1000 | 2 | a[0][0] |
1004 | 5 | a[0][1] |
1008 | 6 | a[0][2] |
1012 | 7 | a[0][3] |
1016 | 12 | a[1][0] |
1020 | 15 | a[1][1] |
1024 | 16 | a[1][2] |
1028 | 17 | a[1][3] |
1032 | 22 | a[2][0] |
1036 | 25 | a[2][1] |
1040 | 26 | a[2][2] |
1044 | 27 | a[2][1] |
Interpretation of 2-D array:
of the array. In this case a[0] itself is an array.
Therefore a points to the entire array a[0]
2-D Array and Pointers
int a[3][4]={{2,5,6,7}, {12,15,16,17}, {22,25,26,27}};
a=1000 | 2 | a[0][0] |
1004 | 5 | a[0][1] |
1008 | 6 | a[0][2] |
1012 | 7 | a[0][3] |
1016 | 12 | a[1][0] |
1020 | 15 | a[1][1] |
1024 | 16 | a[1][2] |
1028 | 17 | a[1][3] |
1032 | 22 | a[2][0] |
1036 | 25 | a[2][1] |
1040 | 26 | a[2][2] |
1044 | 27 | a[2][1] |
Interpretation of 2-D array:
In this case a[0] itself is an array.
Therefore a points to the entire array a[0]
a+1 = address of a + 1 (size of data pointed by a)
= address of a + sizeof(a[0])
= address of a + 16;
2-D Array and Pointers
int a[3][4]={{2,5,6,7}, {12,15,16,17}, {22,25,26,27}};
a=1000 | 2 | a[0][0] |
1004 | 5 | a[0][1] |
1008 | 6 | a[0][2] |
1012 | 7 | a[0][3] |
1016 | 12 | a[1][0] |
1020 | 15 | a[1][1] |
1024 | 16 | a[1][2] |
1028 | 17 | a[1][3] |
1032 | 22 | a[2][0] |
1036 | 25 | a[2][1] |
1040 | 26 | a[2][2] |
1044 | 27 | a[2][1] |
Interpretation of 2-D array name:
Recall that a+i points to an int array of 4 elements.
Therefore, the address given by a+0 is 1000,
a+1 is 1016, and a+2 is 1032
3-D Array Addressing
int a[2][3][2]={2,5,6,7,12,15,16,17,22,25,26,27};
1000 | 2 | a[0][0][0] |
1004 | 5 | a[0][0][1] |
1008 | 6 | a[0][1][0] |
1012 | 7 | a[0][1][1] |
1016 | 12 | a[0][2][0] |
1020 | 15 | a[0][2][1] |
1024 | 16 | a[1][0][0] |
1028 | 17 | a[1][0][1] |
1032 | 22 | a[1][1][0] |
1036 | 25 | a[1][1][1] |
1040 | 26 | a[1][2][0] |
1044 | 27 | a[1][2][1] |
For int a[p] [q] [r];
Address of a[i][j] [k]= address of a[0][0]
+( i * q*r +j *r + k) sizeof_datatype
Similar interpretations extend for any general
n-dimensional array