我对calloc()有了基本的了解。它将内存初始化为0,那么为什么这段代码会这样呢:
table_t *table = calloc(1, sizeof(table_t));
printf("%d", *table);
我希望这段代码打印 0,因为我没有在该内存中放入任何内容(我认为);为什么会出现这个错误?
dictionary.c: In function ‘create_table’:
dictionary.c:11:14: error: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘table_t’ [-Werror=format=]
11 | printf("%d", *table);
| ~^ ~~~~~~
| | |
| int table_t
cc1: all warnings being treated as errors
它会让内存只为int* 0吗?
table
指向的内存将包含零,但会键入printf
格式说明符。
%d
需要 int
而不是 table_t
(假设不能解析为某种类型的 int
)。
尚不清楚代码的预期用途。如果结构体的第一个成员是
int i
那么你可能需要这个:
printf("%d", table->i)