Why is% d for pointer to array giving me the first element of the array?

Can someone explain to me why this code always returns the first element of an array? (7 in this example):

int a[] = {7,1,3};
printf("%d", *a);

      

+3


source to share


6 answers


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. [...]

+11


source


This is not the format the "%d"

first element gives you, it is the dereference operator when used with an array.



Arrays naturally decay to pointers to their first element, and when you look up a pointer, you get the value it points to.

+10


source


*a

will always return the first index of the array. it has nothing to do with %d

.

*a  // returns 7
*(a + 1) // returns 1
*(a + 2) // returns 3  

      

+4


source


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.

+2


source


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|

      

+2


source


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.

+2


source







All Articles