繁体   English   中英

C中的数组数组

[英]Array of arrays of arrays in C

你如何声明一个数组数组? 假设我有一个数组s[] s[0]将包含另一个数组a[]a[0]将包含数组b[] 你会用指针做什么?

// 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.

还有一些选项,其中每个对象都是最高级别的指针,而不是数组。 我没有表现出来。 他们需要为指针指定一些内容。

一个简单的方法。

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层。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM