【问题标题】:C++ Primer - Pointers and References to an ArrayC++ Primer - 指向数组的指针和引用
【发布时间】:2017-01-15 20:15:55
【问题描述】:

在 C++ Primer 第 5 版,第 3.5 节,第 115 页中,它给出了以下示例:

int *ptrs[10]; // ptrs is an array of ten pointers to int
int &refs[10] = /* ? */; // error: no arrays of references
int (*Parray)[10] = &arr; // Parray points to an array of ten ints 
int (&arrRef)[10] = arr; // arrRef refers to an array of ten ints

我理解了几乎所有的例子,除了一个:

int (*Parray)[10] = &arr; // Parray points to an array of ten ints 

要指向一个数组,我可以这样做:

int a[10];
int *p = a;

由于名称“a”也是指向数组的指针,因此现在 p 指向的位置与名称“a”所表示的指针所指的位置相同。

我试图编译本书给出的示例,我期待使用:

int (*Parray)[10] = &arr; // Parray points to an array of ten ints 

将具有与我给出的示例相同的效果。问题是没有发生,这里是代码:

#include <iostream>

using namespace std;

int main(int argc, char const *argv[]) {

    int arr[10] = {1,1,1,1,1,1,1,1,1,1};
    int *ptrs[10];
    int (*Parray)[10] = &arr;
    int (&arrRef)[10] = arr; 

    cout << *(Parray + 1) << endl;
    cout << Parray[1] << endl;

    return 0;
}

此代码编译,并给出以下输出:

0x7fff5c4a2ab8
0x7fff5c4a2ab8

有人能解释一下具体是什么吗:

int (*Parray)[10] = &arr; // Parray points to an array of ten ints

是吗?我能用它做什么?

【问题讨论】:

    标签: c++ arrays pointers reference


    【解决方案1】:

    有人能解释一下具体是什么吗:

    int (*Parray)[10] = &arr; // Parray points to an array of ten ints
    

    是吗?我能用它做什么?

    Parray 的类型是int (*)[10],这意味着它只能分配给指向int [10] 类型对象的指针。

    所以,你所拥有的是一个指向 10 个元素的数组(由 &amp;arr 获得)开头的指针。

    而访问arrParray 的元素的方式将需要您首先取消引用指针Parray 以获得可以索引的arr

    因此做

    (*Parray)[5] = 546;
    

    将第 5 个元素分配给 546,从而得到下面的表达式。

    arr[5] = 546;
    

    here所见。

    【讨论】:

      【解决方案2】:

      它只是指向包含某些元素的数组的指针。 您写道,您可以简单地使用指针int *p = a; 是正确的,但是该指针不仅可以指向某个数组。从技术上讲,指针 p 指向数组中第一个元素的第一个地址。 int(*Parray2)[10] 仅指向具有 10 个元素的数组的指针,因为静态数组签名是 T(&)[N]。 使用数组签名,您可以编写例如可以返回静态数组大小的方法:

      template < size_t N, typename T > size_t GetSize( T(&)[N] )
      {
          return N;
      }
      ...
      int arr[10] = { 1,1,1,1,1,1,1,1,1,1 };
      auto sz = GetSize(arr);
      

      如果你想使用指针,你可以用不同的方式使用它,但是 int(*Parray2)[10] 只能指向包含 10 个元素的数组:

      int arr[10] = { 1,1,1,1,1,1,1,1,1,1 };
      int arr2[11] = { 1,1,1,1,1,1,1,1,1,1,1 };
      
      int *bare_ptr1 = arr; // legal
      int *bare_ptr2 = new int(); // legal
      int *bare_ptr3 = arr2; // legal
      
      int(*Parray)[10] = &arr; // legal
      int(*Parray2)[10] = new int(); // compile error
      int(*Parray3)[10] = arr2; //  compile error
      
      cout << bare_ptr1 << endl;
      cout << &arr[0] << endl;
      cout << *(Parray + 0) << endl;
      cout << Parray[0] << endl;
      

      如果您想查看地址差异,您可以获取 PArray 的地址和数组的地址。这些地址必须不同。

      cout << &Parray << endl; // address of the pointer which points to the array
      cout << &arr << endl; // addres of the first element in the array
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-02-13
        • 2023-03-11
        • 1970-01-01
        • 1970-01-01
        • 2017-08-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多