为什么指向数组的指针(由&array完成)等于数组本身? [重复]

时间:2022-09-13 21:43:36

This question already has an answer here:

这个问题在这里已有答案:

So I almost went all hurr durr on my classmates when they wrote that

所以当他们写这篇文章的时候,我几乎全都投入了我的同学

&array gives you address of the first element

&array为您提供第一个元素的地址

But turns out they are right. This sounds like inconsistency to me. We're talking about array defined like this:

但事实证明他们是对的。这听起来像我的不一致。我们正在谈论像这样定义的数组:

int numbers[] = {1,2,3,4};

The variable numbers is (I think) then of type int* const. I'd think that pointer to that would be int** const. But apparently this expression evaluates as true:

变量数是(我认为)然后是int * const类型。我认为指向它的指针将是int ** const。但显然这个表达式的评估结果为真:

if(&numbers == numbers) {
    printf("Pointer to array is still the same array!\n");
}

And of course, this then also is true:

当然,这也是事实:

int* first_elm_ptr = &numbers;
if(*first_elm_ptr == *numbers)
    printf("%d == %d\n", *first_elm_ptr, *numbers);

So apparently you cannot get a pointer to the variable holding address of that array. Expression &numbers is essentially meaningless. Maybe it is even removed by compiler.

显然你无法获得指向该数组的变量保持地址的指针。表达和数字基本上没有意义。也许它甚至被编译器删除了。

How's that possible? I am very confused right now! How does standard explain this behaviour? I made an ideone test code to verify this: http://ideone.com/pYffYx

怎么可能?我现在很困惑!标准如何解释这种行为?我制作了一个ideone测试代码来验证这一点:http://ideone.com/pYffYx

1 个解决方案

#1


1  

The address of an array and the address of the first element of the array are essentially same value (same address location). They differ in type.

数组的地址和数组的第一个元素的地址基本上是相同的值(相同的地址位置)。他们的类型不同。

Expression &numbers is essentially meaningless

表达和数字基本上没有意义

No, it is not.

不它不是。

In your case,

在你的情况下,

  • &numbers is of type int (*) [4]
  • &number的类型为int(*)[4]

  • numbers is of type int [4], and in some casesNote, it decays to int *.
  • 数字的类型为int [4],在某些情况下注意,它会衰减为int *。


Note:

Quoting C11, chapter §6.3.2.1

引用C11,章节§6.3.2.1

Except when it is the operand of the sizeof operator, the _Alignof operator, or the unary & operator, or is a string literal used to initialize an array, an expression that has type ‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’ that points to the initial element of the array object [....]

除非它是sizeof运算符,_Alignof运算符或一元&运算符的操作数,或者是用于初始化数组的字符串文字,否则具有类型''数组类型''的表达式将转换为表达式键入''指向类型'的指针,指向数组对象的初始元素[...]

#1


1  

The address of an array and the address of the first element of the array are essentially same value (same address location). They differ in type.

数组的地址和数组的第一个元素的地址基本上是相同的值(相同的地址位置)。他们的类型不同。

Expression &numbers is essentially meaningless

表达和数字基本上没有意义

No, it is not.

不它不是。

In your case,

在你的情况下,

  • &numbers is of type int (*) [4]
  • &number的类型为int(*)[4]

  • numbers is of type int [4], and in some casesNote, it decays to int *.
  • 数字的类型为int [4],在某些情况下注意,它会衰减为int *。


Note:

Quoting C11, chapter §6.3.2.1

引用C11,章节§6.3.2.1

Except when it is the operand of the sizeof operator, the _Alignof operator, or the unary & operator, or is a string literal used to initialize an array, an expression that has type ‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’ that points to the initial element of the array object [....]

除非它是sizeof运算符,_Alignof运算符或一元&运算符的操作数,或者是用于初始化数组的字符串文字,否则具有类型''数组类型''的表达式将转换为表达式键入''指向类型'的指针,指向数组对象的初始元素[...]