简体   繁体   English

关于指向数组的指针的困惑

[英]Confusion about pointer to an array

I have a very basic but haunting problem about pointer and array: 关于指针和数组我有一个非常基本但令人难以忘怀的问题:

int main() {

    int a[5] = { 1,2,3,4,5 };
    int(*pa)[5] = &a;

    std::cout << a << std::endl;
    std::cout << &a << std::endl;

    std::cout << pa << std::endl;   
    std::cout << (*pa) << std::endl;

    return 0;
}

Surprisingly, all four outputs give the same address, something like '006AF784' , which means a == &a and pa == *pa . 令人惊讶的是,所有四个输出都给出相同的地址,例如'006AF784' ,这意味着a == &apa == *pa This does not make any sense to me! 这对我没有任何意义!

I understand of course 'a' is the pointer to the first element while '&a' is the pointer to the whole array, so 'a+1' is different from '&a+1' . 我理解当然'a'是指向第一个元素的指针,而'&a'是指向整个数组的指针,因此'a+1''&a+1' But a variable is equal to its address and a pointer is equal to the content which points to is not understandable to me. 但是变量等于它的地址,并且指针等于指向的内容对我来说是不可理解的。 I wonder what is exactly going on within C and compiler. 我想知道在C和编译器中究竟发生了什么。

You pretty much answered your own question. 你几乎回答了自己的问题。 You want to know why a , &a are the same: the first expression designating the array evaluates to a pointer to the first element and the other evaluates to a pointer to the whole thing, as you note. 你想知道为什么a&a是相同的:正如你所注意到的,指定数组的第一个表达式求值为指向第一个元素的指针,另一个表达式指向指向整个元素的指针。 But both are the same address: the first element is at the base address of the array. 但两者都是相同的地址:第一个元素位于数组的基址。 Then why is pa the same? 那为什么pa一样? Why, because you initialized it from &a in its declaration; 为什么,因为你在声明中将其初始化为&a ; it got its value from &a . 它从&a获得了它的价值。 And *pa is the same because pa is a pointer to the array, so *pa is the array. 并且*pa是相同的,因为pa是指向数组的指针,因此*pa是数组。 But an array evaluates to a pointer to the first element: and you have already seen this with a . 但是数组会计算出指向第一个元素的指针:你已经看过这个a The expression *pa designates the same object as a , has the same type and evaluates the same way. 表达*pa表示相同的对象a ,具有相同类型和评价相同的方式。

It's important to understand that a isn't a pointer. 理解a不是指针是很重要的。 In the semantics of C, the array name is convertible to a pointer, but is otherwise just an alias of the address of the first element of the array. 在C的语义中,数组名称可以转换为指针,但在其他方面只是数组第一个元素的地址的别名。

Then when it comes to pa , you're just saying that this pointer should be the address of a , so of course when you print it's value, it should be the same. 然后,当它涉及到pa ,你只是说,这个指针应的地址a ,所以当然,当你打印它的价值,它应该是相同的。 And of course since *pa is an array (its array name), it just aliases its first element's address - which is also a 's. 当然,因为*pa是一个数组(它的数组名称),它只是别名它的第一个元素的地址 - 这也是a

Implicit conversion and "arrays decaying into pointers" is behind this. 隐含的转换和“数组衰减成指针”就在此背后。

Let's draw this array. 我们画这个数组。 Assume that it's stored beginning at address 0x98. 假设它从地址0x98开始存储。

 +———————————————————+
 | 1 | 2 | 3 | 4 | 5 |
 +———————————————————+
 ^
 |
0x98

It should be clear that the address of the array is 0x98. 应该清楚的是,数组的地址是0x98。
It's pretty clear that the address of its first element is also 0x98. 很明显,它的第一个元素的地址也是0x98。

When you're printing 当你打印时

std::cout << a << std::endl;

a is converted into a pointer to its first element – it is equivalent to a被转换为指向其第一个元素的指针 - 它相当于

std::cout << &a[0] << std::endl;

As illustrated above, this has the same numeric value as the pointer to the array. 如上所示,它具有与指向数组的指针相同的数值。

Likewise, when you print 同样,当你打印

std::cout << (*pa) << std::endl;

*pa , being an array, is converted into a pointer to its first element. *pa ,作为一个数组,被转换为指向其第一个元素的指针。

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

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