Kunal Verma Kunal Verma - 2 months ago 9
C Question

Pointer to the Address of Element of an Array

int s[4][2]= {
{1234,56},
{1212,33},
{1434,80},
{1312,78}
};
int i,j;
for(i=0;i<=3;i++)
{
printf("\n");
for(j=0;j<=1;j++)
{
printf("%d ",*(s[i]+j));
}
}


Output Showing is


1234,56
1212,33
1434,80
1312,78



As we know
*(&Variable)
will print the Value of the Variable But when We implement the same concept in above program...

int s[4][2]= {
{1234,56},
{1212,33},
{1434,80},
{1312,78}
};
int i,j;
for(i=0;i<=3;i++)
{
printf("\n");
for(j=0;j<=1;j++)
{
printf("%d ",*(&s[i]+j));
}
}


output is showing the Address of each element of array.

Why this is happening?
Why Output is not equal to value of elements of Array??

Answer

Your array is bi-dimensional. So, s[i] is the address of line i (i.e. s[i] is of type int *), and &s[i] is an address of an address (i.e. of type int **). When you apply * on it, you get an address (i.e. int *).

It is true that the operator & means "address-of" and *(&x) refers to the value of x.

Comments