我目前受困于使用多维数组的遗留代码:
#define B 25
int Table[A][B][C][D][E][F];
我需要用动态值更改 B 常量。问题是,我需要保持表格与以前一样,这样除了重写的分配之外我不会有任何其他东西。
我想听听您对如何做这样的事情的想法/意见。
目前我正在尝试对表的末尾 ([C][D][E]) 进行类型定义,以便在分配时对其进行 malloc,但我遇到了有关该表不是遗留代码所希望的错误...
//int32_t Table[A][B][C][D][E][F];
int32_t* Table[A];
typedef int32_t type_1_t[E][F];
typedef type_1_t type_2_t[C][D];
for (int i = 0; i < A; i++)
Table[i] = (int32_t*) malloc (sizeof (type_2_t) * dynamic_B);
使用这个,我在使用表时得到一个错误(“错误:下标值既不是数组也不是指针也不是 vector ”)。
XXX = Table [a][b][c][d][e][f];
最佳答案
你的类型有误:
int32_t* Table[A];
事实上应该是
int32_t (*Table[A])[C][D][E][F];
或者,当你输入定义的时候
type_2_t *Table[A];
这应该可以解决问题。
关于C : Static to dynamic allocation on multiple dimension array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7469550/