What is Pointer to an Array in C?
int (*p)[4];//p is pointer to array and each array is having 4 its
so,p points to base address of first array ie address of other elements of first array.
p+1 points to base address of second array and not the 1st element of first array.
p+2 points to base address of third array and not the 2nd element of first array.
To access the individual elements of each array:
assign the pointer to array to a pointer to int here pp and access through *(pp+i).
#include<stdio.h>
int main()
{
/*array of strings*/
int arr[3][4]={
{0,1,2,3},/*first string ie first char array*/
{4,5,6,7},
{8,9,1,2},
};
/*pointer to array.Each array is 1-d array of chars ie each array is string*/
int (*p)[4];
p=(int *)arr;/* p=arr+0 so p first points to first char array ie first string Ram*/
int* pp;
/*first pp holds the pointer to the first array*/
pp=p;
printf("\nbase address of first array is:%u,%u\n",pp,&arr[0][0]);
//printf("and first array is:%s\n",pp);//*pp core dump
printf("and first char of first array is:%d\n",*pp);
printf("and second char of first array is:%d\n",*(pp+1));
/* pp now points to second char array ie second string Raheem*/
pp=p+1;
printf("\nbase address of second array is:%u,%u\n",pp,&arr[1][0]);
//printf("and second array is:%s\n",pp);//or *(p+1)
printf("and first char of second array is:%d\n",*pp);
printf("and second char of second array is:%d\n",*(pp+1));
/* pp now ipoints to third char array ie third string Yeshu*/
pp=p+2;
printf("\nbase address of ithird array is:%u,%u\n",pp,&arr[2][0]);
//printf("and third array is:%s\n",pp);//or *(p+2)
printf("and first char of third array is:%d\n",*pp);
printf("and second char of third array is:%d\n",*(pp+1));
printf("\nnow accessing arrays through ptr\n");
int i,j;
for(i=0;i<3;i++)
{
pp=p+i;
for(j=0;j<4;j++)
{
//printf("%dth element of %dth array is=%d \n",j,i,*(pp+j));
printf("%dth array %dth element is %d \n",i,j,*(pp+j));
}
printf("\n");
}
printf("\n\ni.e.\n\n");
for(i=0;i<3;i++)
{
pp=p+i;
for(j=0;j<4;j++)
{
printf("%d ",*(pp+j));
}
printf("\n");
}
printf("\n");
return 0;
}
/*
base address of first array is:3216066912,3216066912
and first char of first array is:0
and second char of first array is:1
base address of second array is:3216066928,3216066928
and first char of second array is:4
and second char of second array is:5
base address of ithird array is:3216066944,3216066944
and first char of third array is:8
and second char of third array is:9
now accessing arrays through ptr
0th array 0th element is 0
0th array 1th element is 1
0th array 2th element is 2
0th array 3th element is 3
1th array 0th element is 4
1th array 1th element is 5
1th array 2th element is 6
1th array 3th element is 7
2th array 0th element is 8
2th array 1th element is 9
2th array 2th element is 1
2th array 3th element is 2
i.e.
0 1 2 3
4 5 6 7
8 9 1 2
*/
int (*p)[4];//p is pointer to array and each array is having 4 its
so,p points to base address of first array ie address of other elements of first array.
p+1 points to base address of second array and not the 1st element of first array.
p+2 points to base address of third array and not the 2nd element of first array.
To access the individual elements of each array:
assign the pointer to array to a pointer to int here pp and access through *(pp+i).
#include<stdio.h>
int main()
{
/*array of strings*/
int arr[3][4]={
{0,1,2,3},/*first string ie first char array*/
{4,5,6,7},
{8,9,1,2},
};
/*pointer to array.Each array is 1-d array of chars ie each array is string*/
int (*p)[4];
p=(int *)arr;/* p=arr+0 so p first points to first char array ie first string Ram*/
int* pp;
/*first pp holds the pointer to the first array*/
pp=p;
printf("\nbase address of first array is:%u,%u\n",pp,&arr[0][0]);
//printf("and first array is:%s\n",pp);//*pp core dump
printf("and first char of first array is:%d\n",*pp);
printf("and second char of first array is:%d\n",*(pp+1));
/* pp now points to second char array ie second string Raheem*/
pp=p+1;
printf("\nbase address of second array is:%u,%u\n",pp,&arr[1][0]);
//printf("and second array is:%s\n",pp);//or *(p+1)
printf("and first char of second array is:%d\n",*pp);
printf("and second char of second array is:%d\n",*(pp+1));
/* pp now ipoints to third char array ie third string Yeshu*/
pp=p+2;
printf("\nbase address of ithird array is:%u,%u\n",pp,&arr[2][0]);
//printf("and third array is:%s\n",pp);//or *(p+2)
printf("and first char of third array is:%d\n",*pp);
printf("and second char of third array is:%d\n",*(pp+1));
printf("\nnow accessing arrays through ptr\n");
int i,j;
for(i=0;i<3;i++)
{
pp=p+i;
for(j=0;j<4;j++)
{
//printf("%dth element of %dth array is=%d \n",j,i,*(pp+j));
printf("%dth array %dth element is %d \n",i,j,*(pp+j));
}
printf("\n");
}
printf("\n\ni.e.\n\n");
for(i=0;i<3;i++)
{
pp=p+i;
for(j=0;j<4;j++)
{
printf("%d ",*(pp+j));
}
printf("\n");
}
printf("\n");
return 0;
}
/*
base address of first array is:3216066912,3216066912
and first char of first array is:0
and second char of first array is:1
base address of second array is:3216066928,3216066928
and first char of second array is:4
and second char of second array is:5
base address of ithird array is:3216066944,3216066944
and first char of third array is:8
and second char of third array is:9
now accessing arrays through ptr
0th array 0th element is 0
0th array 1th element is 1
0th array 2th element is 2
0th array 3th element is 3
1th array 0th element is 4
1th array 1th element is 5
1th array 2th element is 6
1th array 3th element is 7
2th array 0th element is 8
2th array 1th element is 9
2th array 2th element is 1
2th array 3th element is 2
i.e.
0 1 2 3
4 5 6 7
8 9 1 2
*/
No comments:
Post a Comment