How do you declare an array of arrays of arrays? Say I have an array s[]
. s[0]
will contain an other array a[]
and a[0]
will contain an array b[]
. How would you do it with pointers?
質問する
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 に答える