What is difference between array of pointers and pointer to an array?

Answer:
Array of pointers means an array who content are pointers i.e. address of any type of variable. For example:
int * arr[5];  //arr is an array which can hold address of five int variables.
float **arr[10]; //arrr is an array which can hold the address of address of ten float variables.
//Program for array of pointers:
#include<stdio.h>
void main(){
    static int i=5,j=10,k=15,l=20;
    int x;
    int *arr[4]={&i,&j,&k,&l};
    for(x=0;x<4;x++)
         printf("d",*arr[x]);
}


While pointer to an array is a pointer which can hold the address of any array. For example:
int arr[5];
int (*ptr)[5];  //ptr is pointer to such array which size is five and content is int type data.
//Program for pointer to an array:
#include<stdio.h>
void main(){
    int x;
    int arr[3]={10,20,30};
    int (*ptr)[3];
    ptr=arr;
    for(x=0;x<3;x++)
         printf("%d",*ptr++);
}

No comments: