[C language] The difference between the array name and the first address of the array

  • In general, after declaring an array, such as int array[5], the array name array is the first address of the first element of the array, and it is an address constant. However, except in the formal parameter list of the function declaration.
  • In C, in almost all expressions that use arrays, the value of the array name is a pointer constant, which is the address of the first element of the array. Its type depends on the type of the array elements: if they are of type int, then the type of the array name is "constant pointer to int". -"C and pointers"
  • In the following two situations, the array name is not represented by a pointer constant, that is, when the array name is used as the operand of the sizeof operator and the unary operator &. sizeof returns the length of the entire array, not the length of the pointer to the array. Taking the address of an array name produces a pointer to the array, not a pointer to a pointer constant. So the pointer returned after &a is a pointer to an array, which is different from a (a pointer to a[0]) in the type of pointer. -"C and pointers"
  • "+1" is the offset problem: the movement of a pointer of type T is based on sizeof(T).
    That is, array+1: offset by a sizeof(array[0]) unit based on the first address of the first element of the array. The type T here is the first element of an int type in the array. Since the program represents the address result in hexadecimal, the result of array+1 is: 0012FF34+1 sizeof(array[0])=0012FF34+1 sizeof(int)=0012FF38.
    That is &array+1: On the basis of the first address of the array, offset by a sizeof(array) unit. The type T here is an array containing 5 int type elements in the array. Since the program represents the address result in hexadecimal, the result of &array+1 is: 0012FF34+1 sizeof(array)=0012FF34+1 sizeof(int) 5=0012FF48. Note that 1 sizeof(int)*5 (equal to 00000014) can only be added after being converted to hexadecimal.

Guess you like

Origin blog.csdn.net/qq_40463117/article/details/108362935