int a[2][3]; What does "a" actually contain?

C

chandanlinster

consider the program,
/*********************************************************/
int
main()
{
int a[2][3];
printf("a = %u\n", a);
printf("*a = %u\n", *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?)
 
T

tinysisi

I have encounted the same situation.

int main()
{
int a[2][3];
printf("&a=%u\n",&a);
printf("a=%u\n",a);
printf("*a=%u\n",*a);
printf("a[0]=%u\n",a[0]);
printf("&a[0]=%u\n",&a[0]);
printf("a[1]=%u\n",a[1]);
printf("&a[1]=%u\n",&(a[1]));
printf("a[0][0]=%u\n",(a[0][0]));
printf("&a[0][0]=%u\n",&(a[0][0]));
getch();
return 0;
}

The result:

&a=2293584
a=2293584
*a=2293584
a[0]=2293584
&a[0]=2293584
a[1]=2293596
&a[1]=2293596
a[0][0]=2009118740
&a[0][0]=2293584
 
P

Peter Nilsson

chandanlinster said:
consider the program,
/*********************************************************/

#include said:
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.
 
O

Old Wolf

chandanlinster said:
int
main()
{
int a[2][3];
printf("a = %u\n", a);
printf("*a = %u\n", *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;

"a" means &a[0] and "*a" means &a[0][0]. (If you aren't sure
about this, then go to http://c-faq.com/ and read chapter 6).

The first one is the address of an array, the second one is
the address of the first int in that same array -- clearly these
two are at the same location in memory.

NB. %u is not suitable for printing out memory locations,
you should use %p, and cast the argument to void* :

printf("a = %p\n", (void *)a);
 
C

Chris Torek

int a[2][3];
As you can see both "a" and "*a" give the same value; Why is this
happening? What value does "a" acutally contain?

The actual values contained within the object "a" are the six "int"
values you have assigned to the six elements a[0][0], a[0][1],
a[0][2], a[1][0], a[1][1], and a[1][2]. (Since you never assign
any, and "a" is an automatic-duration object, these six values
are all garbage.)

The "value of a" and "value of *a" are pointers, however, due to
The Rule about arrays and pointers in C. See
<http://web.torek.net/torek/c/expr.html#therule> and
<http://web.torek.net/torek/c/pa.html>.
 
A

aarklon

Old said:
chandanlinster said:
int
main()
{
int a[2][3];
printf("a = %u\n", a);
printf("*a = %u\n", *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;

"a" means &a[0] and "*a" means &a[0][0]. (If you aren't sure
about this, then go to http://c-faq.com/ and read chapter 6).

The first one is the address of an array, the second one is
the address of the first int in that same array -- clearly these
two are at the same location in memory.

NB. %u is not suitable for printing out memory locations,
you should use %p, and cast the argument to void* :

printf("a = %p\n", (void *)a);


may be the following exercise might be helpful

which of the following expressions are equivalent to a[ j ][ k ]

a) *( a[ j ] + k )
b) **( a[ j + k ] )
c) ( *( a + j ) )[ k ]
d) ( *( a + k ) )[ j ]
e) *( ( * ( a + j ) ) + k )
f) **( a + j ) + k
g) *( &a[0][0] + j + k)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top