I have this program. And I have some doubts. You can run it in your compiler. I am using gcc compiler in linux
#include<stdio.h>
int main()
{
int j=4,*add;
int i=2;
int a[i][j];
for (i=0;i<=1;i++)
{
for(j=0;j<=3;j++)
{
scanf("%d",&a[i][j],"%d",&a[i][j]);
}
}
for(i=0;i<=1;i++)
{
for (j=0;j<=3;j++)
{
add=&(a[i][j]);
printf("\nSize of %d is %d and address is: %u that should be equal to: %d",a[i][j],sizeof(a[i][j]),&(a[i][j]),add);//Address are not equal while add is having the value of &(a[i][j])
printf("\nSize of %d is %d and value is: %d that should be equal to: %d",a[i][j],sizeof(a[i][j]),*(&(a[i][j])),*add);//Here value at both addresses are same
}
}
printf("\n initial address of the array is: %u that should be equal to address given by &a[0][0]",&a); //And it's equal
return 0;
}
In this code add
occupies the address value of each array elements and prints that address one by one through loop. But address value given by add
is not equal to the one given by &(a[i][j])
while the values give by these two are equal. That is, *add
is equal to *(&(a[i][j]))
for each array element. Can some one explain me why this is so?
I printed the size of each element so as to confirm the sequence of data arrangement in memory. As my compiler 32-bit based, it printed the addresses by the gap of 4 bits in case of both add
and &(a[i][j])
.
In the last I print the initial address of array. This gives the address same as &a[0][0]
. So the question is which method is correct, add=&(a[i][j]
; or direct out putting the a[i][j]
?