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?