Pointer to use Array of Integers in C

How to use pointer to an array of integers:
 int a[]={10,20,30};//array of ints
 int (*p)[3];//p is pointer to array
 so if int *q=p then
 q points to first array {10,20,30}
 q+1 points to second array {garbage here}
 q+2 points to third array {garbage here}

Note:here only one 1-d array so using pointer to array is not useful.
So, generally pointer to array is used with multdimensional arrays

/*
 example of pointer to array, best with 2-d array as 2d array is array of multiple 1-d arrays

 int a2d[3][3]={//rray of three 1-d arrays each having 3 element
     {10,20,30},
     {40,50,60},
     {70,80,90}
 };
 int (*ptr)[3];//ptr is pointer to array of 3 ints so contins address
 ptr=a2d;
 */
 /*first row of 2d array is first 1-d array. ptr+0 contains the base address of first array{10,20,30}; */
 /*second row is second array ptr+2 contains the base address of second array{40,50,60};*/
 /*third row is third array ptr+2 contains the base address of third array{70,80,90};*/
/*
 so incremting ptr++ will give the base address of next array and not the next element of first array and so on
 so check how to access individual elements of array using pointer to array at last of code
 */
#include<stdio.h>
int main()
{
 int a[]={10,20,30};//array of ints
 int (*p)[3];/*p is pointer to array of 3 ints so contins address*/

 p=a;



 printf("\np points to base address of first array of 3 ints=%u\n",p);//3217699276
 int *pp;
 pp=p+0;/*first pp made to hold the pointer to first array*/
 printf("oth element of first array%d\n",*(pp+0));
 printf("1st element of first array%d\n",*(pp+1));
 printf("2nd element of first array%d\n",*(pp+2));

 //p++;
 pp=p+1;/*on incrementing pointer to array p now points to second array which we dont have here*/
 printf("\nNow p points to second array=%u\n",p);/*3217699288(12 more from base address as 3ints*4=12)*/
 printf("oth element of second array%d\n",*(pp+0));/*second array is not present so garbage*/
 printf("1st element of second array%d\n",*(pp+1));/*second array is not present so garbage*/
 printf("2nd element of second array%d\n",*(pp+2));/*second array is not present so garbage*/

 /*Do not surprise by the address after incrementing the pointer to array. Since p is pointer to array of 3 elts so when p is incremented it points to array of 3 elements and not the next element.*/

 //p++;
 pp=p+2;
 printf("\nNow p points to third array=%u\n",p);/*3217699300(12 more from base address as 3ints*4=12)*/
 printf("oth element of third array%d\n",*(pp+0));/*third array is not present so garbage*/
 printf("1st element of third array%d\n",*(pp+1));/*third array is not present so garbage*/
 printf("2nd element of third array%d\n",*(pp+2));/*third array is not present so garbage*/

 /*from the above result we can see that pointer to array is not useful when used with 1-dimensional array so best use of pointer to array is with multi dimensional arrays of ints or string.see below example:*/

 int a2d[3][3]={/*array of three 1-d arrays each having 3 element*/
     {10,20,30},/*first row is first array ptr+0*/
     {40,50,60},/*second row is second array ptr+1*/
     {70,80,90}/*third row is third array ptr+2 */
 };

 int (*ptr)[3];/*ptr is pointer to array of 3 ints so contins address*/

 ptr=a2d;

 printf("\nptr points to first array of 3 ints=%u\n",ptr);//3219896544
 int *qq;
 qq=ptr+0;/*first qq holds address of first 1-d array 10,20,30*/
 printf("oth element of first array%d\n",*(qq+0));
 printf("1st element of first array%d\n",*(qq+1));
 printf("2nd element of first array%d\n",*(qq+2));

// ptr++;
 qq=ptr+1;/*on incrementing pointer to array ptr now points to base address ofsecond array 40,50,60*/
 printf("\nNow p points to second array=%u\n",p);/*3219896556(12 more from base address as 3ints*4=12)*/
 printf("oth element of second array%d\n",*(qq+0));
 printf("1st element of second array%d\n",*(qq+1));
 printf("2nd element of second array%d\n",*(qq+2));

 /*Do not surprise by the address after incrementing the pointer to array. Since ptr is pointer to array of 3 elts so when ptr is incremented it points to second array of 3 elements and not the next element.*/

// ptr++;
 qq=ptr+2;//qq olds the base address of third array
 printf("\nNow p points to third array=%u\n",p);/*3219896568(12 more from base address as 3ints*4=12)*/
 printf("oth element of third array%d\n",*(qq+0));
 printf("1st element of third array%d\n",*(qq+1));
 printf("2nd element of third array%d\n",*(qq+2));

 /*we can always use loops in pointer to array also*/
     printf("\n");
 int i,j;
 for(i=0;i<3;i++)
 {
     qq=ptr+i;
     for(j=0;j<3;j++)
     {
         printf("%dth element of %dth array is=%d \n",j,i,*(qq+j));
     }
     printf("\n");
 }
 return 0;
}

/*
p points to base address of first array of 3 ints=3220992412
oth element of first array10
1st element of first array20
2nd element of first array30

Now p points to second array=3220992412
oth element of second array-1217200140
1st element of second array-1218698699
2nd element of second array-1073974884

Now p points to third array=3220992412
oth element of third array-1073974860
1st element of third array134514601
2nd element of third array-1217200140

ptr points to first array of 3 ints=3220992376
oth element of first array10
1st element of first array20
2nd element of first array30

Now p points to second array=3220992412
oth element of second array40
1st element of second array50
2nd element of second array60

Now p points to third array=3220992412
oth element of third array70
1st element of third array80
2nd element of third array90

0th element of 0th array is=10
1th element of 0th array is=20
2th element of 0th array is=30

0th element of 1th array is=40
1th element of 1th array is=50
2th element of 1th array is=60

0th element of 2th array is=70
1th element of 2th array is=80
2th element of 2th array is=90
*/

No comments:

Post a Comment