简体   繁体   English

数组的值和地址相同,但传递给函数时除外?

[英]The value and address of an array are the same, except when passed to a function?

void pass_arr(int arr[]);

void pass_arr_test()
{
    int arr[5] = {1,2,3,4,5};

    printf( "arr  = %p\n"
            "&arr = %p\n\n", arr, &arr);

    pass_arr(arr);
}

void pass_arr(int arr[])
{
    printf( "passed arr  = %p\n"
            "passed &arr = %p\n\n", arr, &arr);
}

Output: 输出:
arr = 0x28ccd0 arr = 0x28ccd0
&arr = 0x28ccd0 &arr = 0x28ccd0

passed arr = 0x28ccd0 通过的arr = 0x28ccd0
passed &arr = 0x28ccc0 通过&arr = 0x28ccc0


Can someone explain why the value and adress of arr points to the same adress when evaluated in the block where arr was created, but when passed the value and adress point to two different adresses? 有人可以解释为什么在创建arr的块中对arr的值和地址指向相同的地址,但在传递时将值和地址指向两个不同的地址吗?

That's because in the function arr is actually a pointer , not an array. 那是因为在函数中arr实际上是一个指针 ,而不是数组。 Taking the address of a pointer does not yield the same address, the way it does for an array. 获取指针的地址不会产生与数组相同的地址。

Except when it is the operand of the sizeof , _Alignof , or unary & operators, or is a string literal being used to initialize another array in a declaration, an expression of type "N-element array of T " will be converted ("decay") to type "pointer to T ", and the value of the expression will be the address of the first element of the array 1 . 除非它是sizeof_Alignof或一元&运算符的操作数,或者是用于在声明中初始化另一个数组的字符串文字,否则将转换类型为“ T N元素数组”的表达式 (“ decay” ”),然后输入“指向T指针”,表达式的值就是数组1的第一个元素的地址。

When you call 你打电话时

`pass_arr(arr)`;

the expression arr is converted from type "5-element array of int " to "pointer to int ", or int * . 表达式arr从类型“ int 5元素数组”转换为“ pointer to int ”或int *

Note that the address of the first element of the array is the address of the array itself; 注意,数组第一个元素的地址就是数组本身的地址; that's why you get the same value when you print the results of arr and &arr int pass_arr_test , but remember that the types are different; 这就是为什么在打印arr&arr int pass_arr_test的结果时获得相同的的原因,但是请记住类型是不同的; the expression arr is converted to type int * , but &arr has type int (*)[5] ; 表达式arr转换为int *类型,而&arr具有int (*)[5] this matters for things like pointer arithmetic. 这对于像指针算术这样的事情很重要。

Secondly, in the context of a function prototype, declarations of the form T a[] and T a[N] are interpreted as T *a ; 其次,在函数原型的上下文中,形式为T a[]T a[N]被解释为T *a a is actually declared as a pointer instead of an array. a实际上是声明为指针,而不是阵列。 2 2

The important thing to remember is that arrays are not pointers . 要记住的重要一点是, 数组不是指针 Rather, in most contexts, array expressions are converted to pointers as necessary. 相反,在大多数情况下,数组表达式会根据需要转换为指针。


1 - N1570 , 6.3.2.1 Lvalues, arrays, and function designators, ¶ 3 1- N1570,6.3.2.1左值,数组和函数指示符, ¶3
2 - N1570 , 6.7.6.3 Function declarators (including prototypes), ¶ 7 2- N1570,6.7.6.3函数声明符(包括原型),¶7

That is due to pass by value semantics where the arr in pass_arr method is a local pointer variable on the stack whose value is the location of the arr passed from pass_arr_test method. 这是由于pass by value语义,其中pass_arr方法中的arr是堆栈上的本地指针变量,其值是从pass_arr_test方法传递的arr的位置。 So, arr variable in both pass_arr and pass_arr_test are point to the same location and hence the value is same. 因此,pass_arr和pass_arr_test中的arr变量都指向同一位置,因此值相同。 Since they are two different pointers, their memory address is different. 由于它们是两个不同的指针,因此它们的内存地址也不同。 In case of array definition, arr is just an alias to the start of the array and hence it's location and it's value are the same. 在数组定义的情况下,arr只是数组开头的别名,因此它的位置和值是相同的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM