Foo**是一个数组数组?

Pau*_*aul 1 c++ pointers

在C++中,我被告知这Foo** foo;是一个指向指针和数组数组的指针?

有人会详细说明为什么它是一个数组数组或如何解释为这样?

Oli*_*rth 5

它不是一个阵列数组.但是,您可以使用与实际2D数组相同的语法访问各个元素.

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
Run Code Online (Sandbox Code Playgroud)

还有很多其他细微之处.对于更深入的讨论,我强烈建议阅读关于此主题的C FAQ的所有部分:数组和指针(几乎所有这些在C++中同样有效).

但是,在C++中,通常没有理由使用像这样的原始指针.容器类可以更好地处理您的大多数需求,例如std::vector,std::arrayboost::multi_array.