Why the address of array equals array.

  • Thread starter =?big5?B?r0W84Q==?=
  • Start date
?

=?big5?B?r0W84Q==?=

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?
 
C

Chris Dollin

浩澄 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

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.
 
R

Richard Heathfield

?? 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.h> so 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.
 
M

Matthew Zhou

Array name is the representation of the address of the first element
of the array.
 
C

Chris Dollin

Matthew said:
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 `&`.]
 
J

John Bode

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.h> and 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.
 
C

Chris Dollin

Rahul said:
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?)
 
?

=?big5?B?r0W84Q==?=

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 :)
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top