By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 2,015 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

Why the address of array equals array.

P: n/a
Hi All C gurus:
Below is a small program to print out the address of array and
address of array variable:

int main() {
char array[8] = "haha";
printf("array:%x\n", array);
printf("&array:%x\n", &array);
}

The result is :
array:bfffde80
&array:bfffde80

In my thoughts, the "array" and "&array" should be different and why
the
printed out values are the same.
Please help to clarify my doubts? Is there anything I missed in C
language
definition?

Mar 14 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
浩澄 wrote:
Hi All C gurus:
Below is a small program to print out the address of array and
address of array variable:

int main() {
char array[8] = "haha";
printf("array:%x\n", array);
printf("&array:%x\n", &array);
}

The result is :
array:bfffde80
&array:bfffde80

In my thoughts, the "array" and "&array" should be different
They are different, but in your implementation, that difference
isn't visible when you print them out.
and why the printed out values are the same.
(a) you were (un)lucky: what you did has undefined behaviour, since
you're printing a pointer out using a format that expects an
int. Anything can happen (not just in the next half-hour) ...

(b) ... but, assuming that pointers-to-char and ints have the same
size, and that the representation of pointers is just some
canonical address, you printed out two equal pointers and got
to equal answers.

--
Chris "electric hedgehog" Dollin
"We live for the One, you die for the One." Unsaid /Babylon 5/.

Mar 14 '07 #2

P: n/a
?? said:
Hi All C gurus:
Below is a small program to print out the address of array and
address of array variable:

int main() {
char array[8] = "haha";
printf("array:%x\n", array);
printf("&array:%x\n", &array);
}

The result is :
array:bfffde80
&array:bfffde80
You forgot to #include <stdio.hso there is no prototype for the
variadic function, printf - so any output you get is untrustworthy.
Nevertheless, let's run with what you've got...
>
In my thoughts, the "array" and "&array" should be different
Good, because they /are/ different. They have different types. The first
has the type char *, and the second has the type char (*)[8].
and why the printed out values are the same.
For much the same reason that:

1 Tennis Court Road

and

Tennis Court Road

can both be found at 52/12/10N 0/7/15E.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 14 '07 #3

P: n/a
Array name is the representation of the address of the first element
of the array.
Mar 14 '07 #4

P: n/a
On Mar 14, 7:04*am, "浩澄" <houch...@gmail.comwrote:
Hi All C gurus:
* *Below is a small program to print out the address of array and
address of array variable:

int main() {
* * char array[8] = "haha";
* * printf("array:%x\n", array);
* * printf("&array:%x\n", &array);
Others have pointed out the problems here; make sure you #include
<stdio.hand rewrite the print statements as

printf("array: %p\n", (void*) array);
printf("&array: %p\n", (void*) array);

The %p conversion specifier is used to print out pointer values, and
it expects arguments of type void*.
}

The result is :
array:bfffde80
&array:bfffde80

In my thoughts, the "array" and "&array" should be different and why
the
printed out values are the same.
They are different types; however, they wind up being the same value
because the address of the array is the same as the address of the
first element in the array.

Apart from a couple of exceptions, when an array identifier appears in
an expression, its type is converted from "N-element array of T" to
"pointer to T", or T*, and its value is the address of the first
element of the array (&arr[0]).

The exceptions to this rule are when the array identifier is an
operand of either the sizeof or address-of (&) operators. When the
array is an operand of the & operator, the result is of type "pointer
to N-element array of T", or T (*a)[N], and its value is the address
of the base of the array, which is the same as the address of the
first element of the array.
Please help to clarify my doubts? Is there anything I missed in C
language
definition?

Mar 14 '07 #5

P: n/a
Matthew Zhou wrote:
Array name is the representation of the address of the first element
of the array.
No, it is not. The name of an array is /not/ a pointer to its first
element: it is /converted/ to that when it appears in a value context.

[Consider the action of the operators `sizeof` and `&`.]

--
Chris "electric hedgehog" Dollin
"Life is full of mysteries. Consider this one of them." Sinclair, /Babylon 5/

Mar 14 '07 #6

P: n/a
On Mar 14, 7:27 pm, Matthew Zhou <zhouyan1...@gmail.comwrote:
Array name is the representation of the address of the first element
of the array.
Hi,
its like array and &(array+0) or &array[0] are same.
Thanks.

Mar 14 '07 #7

P: n/a
Rahul wrote:
On Mar 14, 7:27 pm, Matthew Zhou <zhouyan1...@gmail.comwrote:
>Array name is the representation of the address of the first element
of the array.

Hi,
its like array and &(array+0) or &array[0] are same.
Yes, exactly like that: both statements are false.

(`&(array+0)` isn't legal; you probably mean to leave
out the `&` or put in `*`, yes?)

--
Chris "starring hedgehog" Dollin
Nit-picking is best done among friends.

Mar 14 '07 #8

P: n/a
Thanks all gurus a lot!
I got it, the address of "pointer of array" should be equal to address
of "the pointer to first array element".
Thought they are in different types!
1 Tennis Court Road
and
Tennis Court Road
is really an interesting way to see the C language :)


Mar 15 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.