chandanlinster wrote:
consider the program,
/************************************************** *******/
#include <stdio.h>
int
main()
{
int a[2][3];
printf("a = %u\n", a);
printf("*a = %u\n", *a);
%u is for unsigned ints, not pointers. So you can't use this
code as the basis for _any_ decent analysis of output.
Use %p but make sure the pointer supplied to printf is a void *.
printf("a = %p\n", (void *) a);
printf("*a = %p\n", (void *) *a);
return 0;
}
/************************************************** *********/
When I compiled and executed the above program, I got the following
output:
a = 3213021068
*a = 3213021068
As you can see both "a" and "*a" give the same value; Why is this
happening? What value does "a" acutally contain? (I mean to whom
does it point to?)
The object a is an array, it is not a pointer. But when used in most
expressions, except as an operator to & or sizeof, an array will
'decay'
to a pointer to it's first element.
Thus the above code is equivalent to...
printf("a = %p\n", (void *) &a[0]);
printf("*a = %p\n", (void *) &(*a)[0]);
Since *a is just a[0], it's also equivalent to...
printf("a = %p\n", (void *) &a[0]);
printf("*a = %p\n", (void *) &a[0][0]);
Why do these two print the same thing? Answer: Why wouldn't they?
The first one prints the address of the first element of a, the second
prints the address of the first sub-element of the first element of a.
Since arrays are not 'padded', these address must be the same.
Note that there is a subtle difference in types, even if the addresses
are the same.
--
Peter