【发布时间】:2020-07-17 01:23:58
【问题描述】:
我正在尝试使用结构数组来创建符号表。这是我到目前为止所拥有的,但是我在创建函数中分配内存时遇到了问题,到目前为止我所拥有的是否正确?
我想要这样的东西作为我对 arr 的最终结果 { {"sym1"; 1}, {"sym2"; 2}, {"sym3"; 3} }
struct str_id {
char* s;
int id;
}
struct symbol_table {
int count;
struct str_id** arr;
}
struct symbol_table *symbol_table_create(void) {
struct symbol_table *stt = malloc(sizeof(struct symbol_table));
stt->count = 1;
stt->arr = malloc(sizeof(struct str_id*) * stt->count);
return stt;
}
【问题讨论】:
-
struct symbol_table { int count = 1; ... }是非法的。您不能在 C 中的类型声明中初始化struct字段。 -
是的,抱歉,我解决了这个问题。其余的都好吗?
-
如果我在创建函数中将其设置为 1,它是如何未定义的
-
哦,我明白你的意思了,我忘了做 stt->count。谢谢
标签: c symbol-table