C Programming language

adplus-dvertising
Array of Pointers
Previous Home Next

We are creating an array of pointers of maximum size 2. Then we have assigned the objects of array pointer.

arr[0] = &a;
arr[1] = &b;

Example

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

Output: The output of the above program would be:

the value of 0=5 , address is 6523

the value of 1=10 , address is 6521

Note: If we write int *p[10]; are we declaring an array of pointers, or a pointer to an array? In declarations, the brackets [] which describe arrays have higher precedence than the * which describes pointers.It looks like the * and the [] are both next to the identifier p, but since [] has higher precedence it means that the brackets are ``closer'' --p is an array first, and what it's an array of is pointers.

If you really wanted a pointer to an array (though usually you do not) you once again override the default precedence using explicit parentheses:

int (*p)[10];

says that p is a pointer first, and what it's a pointer to is an array.

Previous Home Next