C语言——数组名、取数组首地址的区别(1)(C language — difference between array name and first address of array (1))

(1)可以发现array和&array的值是一样的,都是0012FF34。对array+1,结果不是简单的加法那样,即并不是0012FF35,而是0012FF38;由于array[0]和array一样,都是数组首元素的首地址,+1后的结果相同。然而,&array+1的结果却是0012FF48。另外,分别对array和&array求字节长度发现是一样的(结果20是一个十进制数)。

(2)这里会涉及到一下几点知识:

  • 一般情况下声明一个数组之后,比如int array[5],数组名array就是数组首元素的首地址,而且是一个地址常量。但是,在函数声明的形参列表中除外。
  • 在C中, 在几乎所有使用数组的表达式中,数组名的值是个指针常量,也就是数组第一个元素的地址。 它的类型取决于数组元素的类型: 如果它们是int类型,那么数组名的类型就是“指向int的常量指针“。——《C和指针》
  • 在以下两中场合下,数组名并不是用指针常量来表示,就是当数组名作为sizeof操作符和单目操作符&的操作数时。 sizeof返回整个数组的长度,而不是指向数组的指针的长度。 取一个数组名的地址所产生的是一个指向数组的指针,而不是一个指向某个指针常量的指针。所以&a后返回的指针便是指向数组的指针,跟a(一个指向a[0]的指针)在指针的类型上是有区别的。——《C和指针》
  • “+1”就是偏移量问题:一个类型为T的指针的移动,是以sizeof(T)为移动单位。即array+1:在数组首元素的首地址的基础上,偏移一个sizeof(array[0])单位。此处的类型T就是数组中的一个int型的首元素。由于程序是以16进制表示地址结果,array+1的结果为:0012FF34+1*sizeof(array[0])=0012FF34+1*sizeof(int)=0012FF38。

    即&array+1:在数组的首地址的基础上,偏移一个sizeof(array)单位。此处的类型T就是数组中的一个含有5个int型元素的数组。由于程序是以16进制表示地址结果,&array+1的结果为:0012FF34+1*sizeof(array)=0012FF34+1*sizeof(int)*5=0012FF48。注意1*sizeof(int)*5(等于00000014)要转换成16进制后才能进行相加。

————————

(1) Array and & amp; can be found; The values of array are the same, both 0012ff34. For array + 1, the result is not a simple addition, that is, it is not 0012ff35, but 0012ff38; Since array [0] is the first address of the first element of the array like array, the result after + 1 is the same. However, & amp; The result of array + 1 is 0012ff48. In addition, array and & amp; The byte length of array is found to be the same (the result 20 is a decimal number).

(2) The following knowledge will be involved:

  • Generally, 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. 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, that is, the address of the first element of the array. Its type depends on the type of array elements: if they are of type int, the type of array name is “constant pointer to int. -” C and pointer “
  • In the following two cases, the array name is not represented by a pointer constant, or when the array name is used as the operand of sizeof operator and 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. Therefore, the pointer returned after & A is a pointer to the array, which is different from a (a pointer to a [0]) in the type of pointer—— C and pointer
  • “+ 1” is the offset problem: the movement of a pointer of type t takes sizeof (T) as the moving unit. Array + 1: offset by one sizeof (array [0]) unit based on the first address of the first element of the array. 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: offset a sizeof (array) unit based on the first address of the array. Type t here is an array containing 5 int 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.