你如何声明一个数组数组?说我有一个数组s[]
。s[0]
将包含另一个数组a[]
并将a[0]
包含一个数组b[]
。你会如何用指针来做呢?
问问题
189 次
2 回答
4
// b is an array of int. (N is some number.)
int b[N];
// a Option 0: a is an array of M arrays of N int. (M is some number.)
int a[M][N];
// a Option 1: a is an array of M pointers to int.
int *a[M];
a[0] = b;
// Other elements of a must also be assigned in some way.
// s Option 0: s is an array of L arrays of M arrays of N int. (L is some number.)
int s[L][M][N];
// s Option 1: s is an array of L arrays of M pointers to int.
int *s[L][M];
s[0][0] = b;
// Other elements of s must also be assigned in some way.
// s Option 2: s is an array of L pointers to arrays of N int.
int (*s[L])[N];
s[0] = a; // Can use a from a Option 0, not a from a Option 1.
// Other elements of s must also be assigned in some way.
// s Option 3: s is an array of L pointers to pointers to int.
int **s[L];
s[0] = a; // Can use a from a Option 1, not a from a Option 0.
// Other elements of s must also be assigned in some way.
还有一些选项,其中每个对象都是其最高级别的指针,而不是数组。我没有展示这些。他们需要为指针指向定义一些东西。
于 2013-05-02T19:53:28.900 回答
0
一个简单的方法。
int length = 10;
int b[5] = {0,1,2,5,4};
int c[7] = {1,2,3,4,5,6,7};
int** s = malloc(sizeof(int*) * length);
s[1] = b;
s[2] = c;
等等...
此示例适用于 2 层。制作指针s
并进行 ***s
适当的更改,使其成为 3 层。
于 2013-05-02T20:01:44.900 回答