Array of Pointer and Pointer to an Array

An array of Pointer :

  • Syntax :
datatype *variable_name[];      //here in [] number of elements will come

For an example :
int *p[5];  //size of *p[5] will be 20bytes because each pointer has a 4byte
char *ch[3];  //size of *ch[3] will be 12bytes

//int *p[5] = P is an array of 5 interger pointer
//char *ch[3] = ch is an array of 3 character pointer

Example :

#include<conio.h>
#include<stdio.h>
main()
{
        int i = 10, j = 20, k = 30;
        int *p[3];

        p[0] = &i;
        p[1] = &j;
        p[2] = &k;

        printf(" %d %d %d\n", *p[0], *p[1], *p[2]);
}

Answer : 10 20 30

 

Pointer to an Array:

  • Syntax :
datatype (*variable_name)[];  //here in [] number of elements will come

For example :
             int (*p)[3];
             // p is a pointer, pointing to an array of 3 integers
             //size  = 4bytes

             char (*ch)[5];
             //ch is a pointer, pointing to an array of 5 character pointer
             //size = 4bytes

Example :

#include<conio.h>
#include<stdio.h>
main()
{
         int a[5] = {10,20,30,40,50};
         int *ip;
         int (*p)[3];

         ip = a;
          p = a;
 
         printf("ip = %u ip+1 = %u", ip,ip+1);  //%u for viewing address in integer format
         printf("p = %u p+1 = %u", p,p+1);
}

Answer : 1000 1004    //ip will focus on only one member means 4 byte
          1000 1012  //p will focus on 3 member of a means 12bytes

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

Powered by WordPress.com.

Up ↑

%d bloggers like this: