因为多维数组就是数组的数组,因为我们可以这样来:
int ia[3][4]; // array of size 3, each element is an array of ints of size 4 int (*ip)[4] = ia; // ip points to an array of 4 ints ip = &ia[2]; // ia[2] is an array of 4 ints
这样就OK了。*ip 是 int[4] 类型 ——即 ip 是一个指向含有 4 个元素的数组的指针。
需要特别注意的是,加不加括号,是有很大区别的,比如:
int *ip[4]; // array of pointers to int int (*ip)[4]; // pointer to an array of 4 ints
两者是完全不同的两种东西,具体可以参考示例:
1 #include <iostream> 2 3 using namespace std; 4 5 int main() 6 { 7 int i = 11; 8 int j = 12; 9 int x = 13; 10 int y = 14; 11 12 int* p1 = &i; 13 int* p2 = &j; 14 int* p3 = &x; 15 int* p4 = &y; 16 17 int a2[3][4] = {21,22,23,24}; 18 int *p5[4] = {p1, p2, p3, p4}; 19 int (*p6)[4] = a2; 20 21 cout << p5 << endl; 22 cout << *p5 << endl; 23 cout << *p5[0] << endl; 24 cout << p6 << endl; 25 cout << *p6 << endl; 26 cout << *p6[0] << endl; 27 cout << *(p6 + 1) << endl; 28 }
输出结果:
svpm-dev# g++ foo.cpp -o foo svpm-dev# ./foo 0xbfb2084c 0xbfb20868 11 0xbfb2081c 0xbfb2081c 21 0xbfb2082c
原文:http://www.cnblogs.com/hustcser/p/3628699.html