Foo**如何成为数组的数组? 在C++中,我被告知了这个词;是指向指针的指针,也是数组的数组

Foo**如何成为数组的数组? 在C++中,我被告知了这个词;是指向指针的指针,也是数组的数组,c++,pointers,C++,Pointers,有人能详细解释一下为什么它是一个数组数组,或者如何解释它吗?它实际上不是一个数组数组。但是,可以使用与实际二维数组相同的语法访问各个元素 int x[5][7]; // A real 2D array // Dynamically-allocated memory, accessed through pointer to pointer // (remember that all of this needs to be deallocated with symmetric delete[]

有人能详细解释一下为什么它是一个数组数组,或者如何解释它吗?

它实际上不是一个数组数组。但是,可以使用与实际二维数组相同的语法访问各个元素

int x[5][7];   // A real 2D array

// Dynamically-allocated memory, accessed through pointer to pointer
// (remember that all of this needs to be deallocated with symmetric delete[] at some point)
int **y = new int*[5];
for (int i = 0; i < 7; i++) {
    y[i] = new int[7];
}

// Both can be accessed with the same syntax
x[3][4] = 42;
y[3][4] = 42;

// But they're not identical.  For example:

void my_function(int **p) { /* ... blah ... */ }

my_function(x);  // Compiler error!
my_function(y);  // Fine
还有很多其他微妙之处。为了更深入的讨论,我强烈建议阅读C FAQ的所有部分,关于这个主题:几乎所有的C++都是同样有效的。
<>但是,在C++中,使用这种指针的原因很少。容器类可以更好地满足您的大多数需求,例如std::vector、std::array或boost::multi_array。

它实际上不是数组数组。但是,可以使用与实际二维数组相同的语法访问各个元素

int x[5][7];   // A real 2D array

// Dynamically-allocated memory, accessed through pointer to pointer
// (remember that all of this needs to be deallocated with symmetric delete[] at some point)
int **y = new int*[5];
for (int i = 0; i < 7; i++) {
    y[i] = new int[7];
}

// Both can be accessed with the same syntax
x[3][4] = 42;
y[3][4] = 42;

// But they're not identical.  For example:

void my_function(int **p) { /* ... blah ... */ }

my_function(x);  // Compiler error!
my_function(y);  // Fine
还有很多其他微妙之处。为了更深入的讨论,我强烈建议阅读C FAQ的所有部分,关于这个主题:几乎所有的C++都是同样有效的。 <>但是,在C++中,使用这种指针的原因很少。容器类可以更好地满足您的大多数需求,例如std::vector、std::array或boost::multi_array。

它不是数组数组,但您可以通过以下方式将Foo**构建为数组数组:

Foo** arr = new Foo*[height];
for (int i = 0; i < height; ++i)
    arr[i] = new Foo[width]; // in case of Foo has default constructor
它也可以是指向Foo类型指针的指针

它不是数组数组,但您可以通过以下方式将Foo**构造为数组数组:

Foo** arr = new Foo*[height];
for (int i = 0; i < height; ++i)
    arr[i] = new Foo[width]; // in case of Foo has default constructor
它也可以是指向Foo类型指针的指针


它不是数组的数组-指针不是数组,因此指向指针的指针不是指向数组的数组


它们也可以类似地被编入索引以存储和检索信息……因此,它们在功能上与数组非常相似。

这不是数组数组数组-指针不是数组,因此指向指针的指针不是数组到数组的指针


它们可以被类似地索引为存储和检索信息……因此它们的功能与数组的大小类似。

简单答案:在C++中,一个可变大小,即没有int大小的INT(5)数组,只是指向数组的第一个元素的指针。因此,编译器无法区分指针是指向数组的开头还是指向单个实例。因此,编译器始终允许您将指针视为数组。但是,如果指针没有指向一个足够大的内存块,它将被用作数组,这样会导致某种内存故障分割错误或静默失败。

< P>简单答案:在C++中,一个可变大小,即没有INT(5)数组的固定大小,只是指向数组的第一个元素的指针。因此,编译器无法区分指针是指向数组的开头还是指向单个实例。因此,编译器始终允许您将指针视为数组。但是,如果指针没有指向足够大的内存块以用作数组,那么这样使用它将导致某种内存故障、分段故障或静默故障。

指针不是数组,它们可以像数组一样使用。Foo**如何成为数组数组?不是。指针不是数组,它们可以像数组一样使用。Foo**是数组的数组吗?不是。你会用什么语法访问arr的各个元素?@Paul就像二维数组名[index1][index2]-我已经更新了应答器你会用什么语法访问arr的各个元素?@Paul就像二维数组名[index1][index2]-我已经更新了应答器,@MooingDuck:的确如此。谢谢对于每一个新的,都应该有一个删除!:我的学生们对此怎么说都不够还有@MooingDuck:真的。谢谢对于每一个新的,都应该有一个删除!:我的学生们对此怎么说都不够