S
somenath
Hi All,
I have couple of questions regarding the behavior of the bellow code.
#include<stdio.h>
int main(void)
{
int ar[][3]={
{1,2},
{3,4},
{4,5}
};
printf("\n sizeof(int) = %d\n",sizeof(int));
printf("\n addr ar = %p \n",(void *) ar);
printf("\n sizeof(ar) = %d\n",(int) sizeof(ar));
printf("\n addr ar+1 = %p \n",(void *) (ar+1));
printf("\n addr *ar+1 = %d \n", **(ar+1));
printf("\n addr *ar = %d \n", **ar);
return 0;
}
Out put
sizeof(int) = 4
addr ar = 0xbfa21a3c
sizeof(ar) = 36
addr ar+1 = 0xbfa21a48
diff between (ar+1) and ar = 1
addr *ar+1 = 3
addr *ar = 1
According to my understanding is type of ar is int (* )[3][3] that's
the reason sizeof (ar) prints 3*3*sizeof(int) = 9*4 = 36;
So as according to pointer arithmetic ar+1 should point to next object
of type int (* )[3][3] .But in the current program address of ar is
0xbfa21a3c and address of ar+1 is 0xbfa21a48 .The diffrence between ar
+1 and ar is 3 *sizeof(int) .
Which indicates ar is of type int(*)[] not int (*)[][] .
How is it possible? Where am I going wrong?
Please help.
Regards,
Somenath
I have couple of questions regarding the behavior of the bellow code.
#include<stdio.h>
int main(void)
{
int ar[][3]={
{1,2},
{3,4},
{4,5}
};
printf("\n sizeof(int) = %d\n",sizeof(int));
printf("\n addr ar = %p \n",(void *) ar);
printf("\n sizeof(ar) = %d\n",(int) sizeof(ar));
printf("\n addr ar+1 = %p \n",(void *) (ar+1));
printf("\n addr *ar+1 = %d \n", **(ar+1));
printf("\n addr *ar = %d \n", **ar);
return 0;
}
Out put
sizeof(int) = 4
addr ar = 0xbfa21a3c
sizeof(ar) = 36
addr ar+1 = 0xbfa21a48
diff between (ar+1) and ar = 1
addr *ar+1 = 3
addr *ar = 1
According to my understanding is type of ar is int (* )[3][3] that's
the reason sizeof (ar) prints 3*3*sizeof(int) = 9*4 = 36;
So as according to pointer arithmetic ar+1 should point to next object
of type int (* )[3][3] .But in the current program address of ar is
0xbfa21a3c and address of ar+1 is 0xbfa21a48 .The diffrence between ar
+1 and ar is 3 *sizeof(int) .
Which indicates ar is of type int(*)[] not int (*)[][] .
How is it possible? Where am I going wrong?
Please help.
Regards,
Somenath