Just when I had relaxed thinking I have a fair understanding of pointers in the context of arrays,I have fallen face down again over this following program.I had understood how for an array arr
,arr
and &arr
are both same in magnitude,but different in type,but I fail to get a solid grip over the following program's output.I try to visualize it but succeed only partially.I would appreciate if you can give a rigorous and detailed explanation for this thing so that guys like me can be done with this confusion for good.
In the following program,I have used a "2D" array demo[][2]
.I know that demo[]
will be an array of arrays
of size 2.I also know that demo
used alone will be of type (*)[2]
.Still I am at a loss about the following :
1) Why is &demo[1]
same as demo[1]
?Isn't demo[1]
supposed to be the address of the second array?What on earth is &demo[1]
then and why is it same as address of the second array?
2) I know that the second printf()
and fourth are same,as demo[1]
is nothing else but *(demo+1)
.But I've used it so to illustrate this point.How can it be equal to the third printf()
,ie,how can demo+1 be equal to *(demo+1)? demo[1]
being the same as *(demo+1)
is well-known,but how can demo+1
be equal to *(demo+1)
? How can "something" be equal to the value at that "something"?
3) And since it just proved I am not very smart,I should stop my guessing game and ask you for a conclusive answer about what are the types for the following :
&demo[1]
demo[1]
demo+1
#include<stdio.h>
int main(void)
{
int demo[4][2]= {{13,45},{83,34},{4,8},{234,934}};
printf("%p\n",&demo[1]);
printf("%p\n",demo[1]); //Should have cast to (void*),but works still
printf("%p\n",demo+1);
printf("%p\n",*(demo+1));
}
OUTPUT:
0023FF28
0023FF28
0023FF28
0023FF28