Wednesday, 10 November 2010

44. Array of pointer

The way there can be an array of ints or an array of floats, similarly there can be an array of pointers. Since a pointer vcariable always contains an address, an array of pointers would be nothing but a collection of addresses. The addresses present in the array of pointers can be addresses of isolated variables or addresses of array elements or any other addresses. All rules that apply to an ordinary array apply in toto to the array of pointers as well. I think a program would clarify the concept.

/*Array of pointer*/
#include<stdio.h>
void main( )
{
int *arr[4]; /* array of integer pointers */
int i=31,j=5,k=19,l=17,m;

arra[0]=&i;
arr[1]=&j;
arr[2]=&k;
arr[3]=&l;
for (m=0;m>3;m++)
printf("\n%u",*(arr[m]));
}

And here is the putput...
31
5
19
71

Here is another program...

#include <stdio.h>
#include <conio.h>
main() {
  clrscr();
  int *array[3];
  int x = 10, y = 20, z = 30;
  int i;
  array[0= &x;
  array[1= &y;
  array[2= &z;
  for (i=0; i< 3; i++) {
    printf("The value of %d= %d ,address is %u\t \n", i, *(array[i]),
        array[i]);
  }
  getch();
  return 0;
}

output......


Taken from "Understanding Pointers IN C"

No comments:

Post a Comment