I am having a tough time understanding the type and use of the name of the array in C. It might seems a long post but please bear with me.
I understand that the following statement declares a
to be of type int []
i.e array of integers.
int a[30];
While a
also points the first element of array and things like *(a+2)
are valid. Thus, making a
look like a pointer to an integer. But actually the types int []
and int*
are different; while the former is an array type and later is a pointer to an integer.
Also a variable of type int []
gets converted into a variable of type int*
when passing it to functions; as in C
arrays are passed by reference (with the exception of the sizeof
operator).
Here comes the point which makes me dangle. Have a look at the following piece of code:
int main()
{
int (*p)[3];
int a[3] = { 5, 4, 6 };
p = &a;
printf("a:%d\t&a:%d\n",a,&a);
printf("%d",*(*p + 2));
}
OUTPUT:
a:2686720 &a:2686720
6
So, how does the above code work? I have two questions:
a
and&a
have the same values. Why?- What exactly does
int (*p)[3];
do? It declares a pointer to an array, I know this. But how is a pointer to an array different from the pointer to the first element of the array and name of the array?
Can anyone clarify things up? I am having a hell of a lot of confusions.
I know that I should use %p
as a placeholder instead of using %d
for printing the value of pointer variables. As using the integer placeholder might print truncated addresses. But I just want to keep things simple.
They have the same value but different types. Array objects have no padding between elements (before or after) so the address of the array and the address of the first element of the array are the same.
That is:
These are two different pointer types. Take for example, pointer arithmetic:
EDIT: due to popular demand I added below some information about conversion of arrays.
With three exceptions, in an expression an object of type array of
T
is converted to a value of type pointer toT
pointing to the first element of the array. The exceptions are if the object is the operand ofsizeof
or&
unary operator or if the object is a string literal initializing an array.For example this statement:
is actually equivalent to:
Also please note that
d
conversion specifier can only be use to print a signed integer; to print a pointer value you have to usep
specifier (and the argument must bevoid *
). So to do things correctly use:respectively:
As,
a --> pointer pointing to starting element of array a[],it does not know about other element's location.
.&a --->address location for storing array a[] which stores first element location,but knows every element's location
.Similarly,other elements location will be (a+2),(a+4) and so upto the end of the array.
Hence,you got such result.
a and &a have the same value because a long time ago you were required to use the address operator & on arrays to get the array's address, but it is no longer necessary. The name of the array (a in this case) these days just represents the memory address of the array itself, which is also what you get from &a. It's a shorthand that the compiler handles for you.
From C99 Standard n1124 6.3.2.1 p3
Other answers already explained the issue. I am trying to explain it with some diagram. Hope this will help.
When you declare an array
the memory arrangement looks like
Now answering your question:
As you already know that
a
is of array type and array namea
becomes a pointer to first element of arraya
(after decay),i.e it points to the address0x100
. Note that0x100
also is the starting address of the memory block (arraya
). And you should know that, in general, the address of the first byte is said to be the address of the variable. That is, if a variable is of 100 bytes, then its address is equal to the address of its first byte.&a
is address of the entire memory block, i.e it is an address of arraya
. See the diagram:Now you can understand why
a
and&a
both have same address value although both are of different type.See the above figure, it is explained clearly how pointer to an array is different from the pointer to an array element.
When you assign
&a
top
, thenp
points to the entire array having starting address0x100
.NOTE: Regarding to the line
In C, arguments are passed by value. No pass by reference in C. When an ordinary variable is passed to a function, its value is copied; any changes to corresponding parameter do not affect the variable.
Arrays are also passed by value, but difference is that the array name decays to pointer to first element and this pointer assigned to the parameter (here, pointer value is copied) of the function; the array itself isn't copied.
In contrast to ordinary variable, an array used as an argument is not protected against any change, since no copy is made of the array itself, instead copy of pointer to first element is made.
You should also note that
sizeof
is not a function and array name does not act as an argument in this case.sizeof
is an operator and array name serves as an operand. Same holds true when array name is an operand of the unary&
operator.In answer to question 1, this is simply an aspect of the C language as designed, unlike most other modern languages C/C++ allows direct manipulation of addresses in memory and has built in facilities to 'understand' that. There are many articles online that explain this better than I could in this small space. Here is one and I am sure there are many others: http://www.cprogramming.com/tutorial/c/lesson8.html