Why is% d for pointer to array giving me the first element of the array?
First, let me tell you that not a pointer to an array here , all you have is an array variable that the dereference operator is applied to.
Arrays (among many other cases) using as operands operators *
fall into a pointer to the first element Note . So,
printf("%d", *a);
coincides with
printf("%d", *(&(a[0]));
thereafter, as required printf()
,
- You have a conversion specifier
%d
- You have an argument like
int
So it prints the value correctly.
Note:
Quote C11
, chapter §6.3.2.1, Lvalues, Arrays and Function Notation, (emphasis mine)
Unless it is an operator operand, an operator
sizeof
,_Alignof
or a unary&
or string literal used to initialize an array, an expression that is of type `` of type type is converted to an expression with a type pointer '' to type such points to the original element of the array object and not is an lvalue. [...]
source to share
From the C standard (6.3.2.1 Lvalues, Arrays and Function Notation)
3 Unless it is the operand of the sizeof operator or the unary & operator, or is a string literal used to initialize an array, an expression that is of type `` array of type is converted to an expression of type, '' a pointer to type pointing to the starting element of the object array and is not an lvalue. If the array object has a register storage class, the behavior is undefined.
and (6.5.3.2 Address and Indirection Operators)
4 The unary * operator denotes indirection. If the operand points to a function, the result is a function designator; if it points to object, the result is an lvalue denoting the object. ... If the operand is of type pointer to type, the result is of type. If an invalid value was assigned to a pointer, the behavior of the unary * operator was not found.
So in this statement
printf("%d", *a);
the array a
in the expression is *a
converted to a pointer to its first element, and applying the unary operator * yields the object (first element of the array) that the pointer points to.
source to share
printf("%d", *a);
* a is equivalent to * (a + 0); which gives you the first element of the array and is also equivalent to [0];
a is the address of the array. Initially, the array starts at 0x00; a + 0 gives you the same number, since you are incrementing it by 0. If you write + 1 you get 0x04 if int is 4 bytes. Addig * is just playing it out. Finally, you get a number at offset 4, which is the second int element in the array.
-----------------------------------------
| 7 | 1 |
-----------------------------------------
|0x00|0x01|0x02|0x03|0x04|0x05|0x06|0x07|
source to share
Coming from the inline background, I always think of data in physical terms. Thus, every physical thing has two main functions associated with it.
- What? (This is the value of the variable).
- Where? (The physical address where the variable is stored).
Now when you declare an array, you've created a list of variables on the stack named "a", where "a" tells the start of where the (array address) part
When using use
printf("%d", *a);
you are actually asking the program to go to the beginning of the array you select 7.
"a" is basically the name you passed to the array of numbers, and it always points to the beginning of the array, and hence when you use it directly, it gives you the first value.
source to share