Oct 4, 2014

C Programming #49: Pointer to array

In this article we will discuss regarding pointer to array. "Array of pointer" and "pointer to array" looks like a word play. Most of them confuse it to be same, but they are not the same.


NOTE there are some related article

  1. C Programming #39: Array - single dimension
  2. C Programming #41: Pointer - introduction
  3. C Programming #47: Array of pointers 
  4. C Programming #48: Pointers and Array - brothers 
Please review above article so that related items and understood well with comparison.

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.


  1. While address of arr[0] is 0xbfe79434 and it is pointer to integer.
  2. While address of arr is 0xbfe79434 and it is pointer to array of integer with 3 elements.
Lets declare these two pointer and see how this can be used. So that we could understand pointer to integer and pointer to integer array.

#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;
}
Output of the above program is

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
  1. Declaration of pointer to array.
    1. int (*pa)[3] - pa is pointer to array with 3 elements.
    2. Brackets are needed, otherwise int *pa[3] - would mean array of integer pointer.
  2. int *p; is pointer to integer, which can point to individual element of the array. 
  3. int (*pa)[3]; is pointer to array, it points to array as a whole, it can be used to access whole of array also.
  4. Note using p we could have accessed array using as p[0], p[1], p[2].
  5. But using pa we can access array as p[0][0], p[0][1],p[0][2].
    1. This is confusing part. did we have multidimensional array ?
    2. pa is pointer to array.
    3. pa[0] is pointer to arr
    4. pa[1] would point to next array, which is not even valid memory.
  6. 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 - inception
Previous Article - C Programming #48: Pointers and Array - brothers
All Article - C Programming

No comments :

Post a Comment