array <-> pointer Question

H

herrcho

#include <stdio.h>

int multi[2][4];

int main()
{
printf("\nmulti = %u",multi);
printf("\nmulti[0] = %u",multi[0]);
printf("\n&multi[0][0] = %u\n",&multi[0][0]);
return 0;
}

the 3 statements above shows the same result.
i can understand the second and the third result in same output.

BUT

the first and the second ..
as far as i know, multi is the pointer to the first element of the
array,
so multi is same as &multi[0]
then, why multi == multi[0]

please teach me the right thing.. TIA ^^
 
J

Joona I Palaste

herrcho said:
#include <stdio.h>
int multi[2][4];
int main()
{
printf("\nmulti = %u",multi);

multi decays into a pointer to an array of 4 ints here.
printf("\nmulti[0] = %u",multi[0]);

multi[0] decays into a pointer to an int here.
printf("\n&multi[0][0] = %u\n",&multi[0][0]);

&multi[0][0] is the same thing as multi[0].
return 0;
}

Technically you are invoking undefined behaviour by printing pointers
with %u. You should use %p and cast the arguments to (void *).
But your compiler might well ignore this and print the correct addresses
anyway.
the 3 statements above shows the same result.
i can understand the second and the third result in same output.

the first and the second ..
as far as i know, multi is the pointer to the first element of the
array,
so multi is same as &multi[0]
then, why multi == multi[0]
please teach me the right thing.. TIA ^^

This is because multi and multi[0] both decay into pointers when given
as parameters, and the first element of an array is required to begin
at the same address as the array itself.

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"I am not very happy acting pleased whenever prominent scientists overmagnify
intellectual enlightenment."
- Anon
 

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
474,079
Messages
2,570,575
Members
47,207
Latest member
HelenaCani

Latest Threads

Top