我正在尝试在结构Like this中初始化数组。
我希望数组具有row * col的大小。
这是代码的一部分:
struct tbl{
int col;
int row;
char** elem [col*row];
};
int main(int argc, char** argv){
int i, row, col;
col = row = 0;
bool loop = true;
char c;
col = col/row;
table tab;
tab.row = row;
tab.col = col;
return 0;
}
最佳答案
您不能以这种方式声明结构。人们所做的一件事是将其数组设置为该结构的末尾,然后使用malloc为该结构中的数组保留额外的空间。像这样:
typedef struct {
int row;
int col;
char **elem[];
} table;
void some_func() {
int row = 5;
int col = 5;
table *tab = malloc(sizeof(table) + ((row * col) * sizeof(char **)));
tab->row = row;
tab->col = col;
// now do whatever you need to with your new struct
// that holds an array of 25 char ** elements
}