NOTE there are some related article
- C Programming #39: Array - single dimension
- C Programming #41: Pointer - introduction
- C Programming #47: Array of pointers
- C Programming #48: Pointers and Array - brothers
Pointer to array is type of pointer that points to array. We know that array is series of same type item. We already know that each element of the array has address associated with it. Even the array as whole has the address associated with it. Let me give a example to point it out.
#include <stdio.h> int main() { int arr[3]; printf("Address of arr[0] is %p\n", &arr[0]); printf("Address of arr[1] is %p\n", &arr[1]); printf("Address of arr[2] is %p\n", &arr[2]); printf("Address of arr is %p \n", &arr); return 0; }
Output of the above program is
Address of arr[0] is 0xbfe79434 Address of arr[1] is 0xbfe79438 Address of arr[2] is 0xbfe7943c Address of arr is 0xbfe79434
If you see that address of arr[0] and arr is same, Actually not exactly. Note always pointer is just not associated with 32bit or 64 bit number but also type.
- While address of arr[0] is 0xbfe79434 and it is pointer to integer.
- While address of arr is 0xbfe79434 and it is pointer to array of integer with 3 elements.
#include <stdio.h> int main() { int arr[3] = {33, 2, 7}; int *p; int (*pa)[3]; p = &arr[0]; pa = &arr; printf("Address of arr[0] is %p\n", &arr[0]); printf("Address of arr[1] is %p\n", &arr[1]); printf("Address of arr[2] is %p\n", &arr[2]); printf("Address of arr is %p \n", &arr); printf("p has address %p\n", p); printf("pa has address %p\n", pa); /* Accessing the array using p */ printf("Sizeof of p is %d\n", sizeof(p)); printf("Array using p is %d %d %d\n", p[0], p[1], p[2]); /* Accessing the array using pa */ printf("Sizeof of pa is %d\n", sizeof(pa)); printf("Array using pa is %d %d %d\n", pa[0][0], pa[0][1], pa[0][2]); printf("Array using pa is %d %d %d\n", *(*pa+0), *(*pa+1), *(*pa+2)); return 0; }
Address of arr[0] is 0xbfc7958c Address of arr[1] is 0xbfc79590 Address of arr[2] is 0xbfc79594 Address of arr is 0xbfc7958c p has address 0xbfc7958c pa has address 0xbfc7958c Sizeof of p is 4 Array using p is 33 2 7 Sizeof of pa is 4 Array using pa is 33 2 7 Array using pa is 33 2 7
Lesson learnt from the above program is
- Declaration of pointer to array.
- int (*pa)[3] - pa is pointer to array with 3 elements.
- Brackets are needed, otherwise int *pa[3] - would mean array of integer pointer.
- int *p; is pointer to integer, which can point to individual element of the array.
- int (*pa)[3]; is pointer to array, it points to array as a whole, it can be used to access whole of array also.
- Note using p we could have accessed array using as p[0], p[1], p[2].
- But using pa we can access array as p[0][0], p[0][1],p[0][2].
- This is confusing part. did we have multidimensional array ?
- pa is pointer to array.
- pa[0] is pointer to arr
- pa[1] would point to next array, which is not even valid memory.
- To explain the point 5 let me take an example of multidimensional array and explain it.
#include <stdio.h> int main() { int a[3][2] = {{3,4},{7,8},{5,1}}; int (*pa)[2]; pa = &a[0]; printf("Elements are pa[0][0] - %d, pa[0][1] - %d\n", pa[0][0], pa[0][1]); printf("Elements are pa[1][0] - %d, pa[1][1] - %d\n", pa[1][0], pa[1][1]); printf("Elements are pa[2][0] - %d, pa[2][1] - %d\n", pa[2][0], pa[2][1]); return 0; }
Output of the above program is
Elements are pa[0][0] - 3, pa[0][1] - 4 Elements are pa[1][0] - 7, pa[1][1] - 8 Elements are pa[2][0] - 5, pa[2][1] - 1
Links
Next Article - C Programming #50: Double pointer - inceptionPrevious Article - C Programming #48: Pointers and Array - brothers
All Article - C Programming
No comments :
Post a Comment