Array of Pointers


The general syntax for array of pointers is :


Here data_type refers to thedata type of the array whose name is array. The exp1 is the positive valued integer expression that indicates the maximum of elements associated with each. For example:
                                            
  int *a[3];

Lest us consider a program  to understand array of pointers:
#include <stdio.h>
#include <conio.h>
main ( )
{
            int *a[3];
            int i, j;
            clrscr ( );
            for (i=0; i<3; i++)
            a [i] = (int *) malloc (4*sizeof (int));
            printf (“\n Enter elements :\n”);
            for (i=0; i<3; i++)
                        for (j=0; j<4; j++)
                                    scanf (“%d”,a[i] + j);
            printf (“Information relating to array is :\n”);
            for (i=0; i<3; i++)
            {
                        printf (“\n \n Address of Row[%d] =%u”, i, a[i] );
                        for (j=0; j<4; j++)
                        printf (“\n a[%d][%d] =%d and its address =%u”, i, j, *(a[i]+j), (a[i]+j));
            }
getch( );
}

Output:
Enter elements :
1          2          4          7
2          3          5          3
9          11        12        9
Information relating to array is :
Address of Row[0] =1940
a[0][0] = 1 its address = 1940
a[0][1] = 1 its address = 1942
    .                          .
    .                          .
    .                          .
a[2][3] = 9 its address = 1978

Previous                                                                                    Next


Powered by Blogger.