pointer confusion - error: passing argument 1 of ‘value’ from incompatible pointer type; note: expected ‘...’ but argument is of type ‘...'
pointer confusion - error: passing argument 1 of ‘value’ from incompatible pointer type; note: expected ‘...’ but argument is of type ‘...'
编辑:这太明显了,我不知道我怎么没注意到。谢谢你们的帮助,伙计们!
我正在使用 C 中的二叉树制作一个符号 table,但我在定义基本函数时遇到了困难。我一直无法完全掌握指针操作,这些错误让我头疼,我不知道如何解决它们,尽管我相信你们中的许多人会觉得我的问题微不足道。
我有一个这样定义的结构:
typedef struct SymTable {
symbol_t *rootNode;
} symTable_t;
像这样的初始化函数:
void initTable(symTable_t *table) {
table->rootNode = NULL;
}
在我的 main.c 中调用函数:
symTable_t *newTable = malloc(sizeof(symTable_t));
// check if malloc was successful
initTable(&newTable);
当我尝试翻译时它抛出这两个错误:
main.c:12:12: error: passing argument 1 of ‘initTable’
from incompatible pointer type [-Werror=incompatible-pointer-types]
initTable(&newTable);
^
In file included from main.c:1:0:
sym_tab.c:18:7: note: expected ‘symTable_t * {aka struct SymTable *}’ but argument is of type ‘symTable_t ** {aka struct SymTable **}’
void initTable(symTable_t *table) {
当我像 _initTable(symTable_t table)_ 一样定义 initTable() 时,它只会抛出更多错误。
提前谢谢你。
您传递的是指向 symTable_t
的指针,而不仅仅是指向 symTable_t
的指针。
去掉这一行的&
:
initTable(&newTable);
错误将消失。
编辑:这太明显了,我不知道我怎么没注意到。谢谢你们的帮助,伙计们!
我正在使用 C 中的二叉树制作一个符号 table,但我在定义基本函数时遇到了困难。我一直无法完全掌握指针操作,这些错误让我头疼,我不知道如何解决它们,尽管我相信你们中的许多人会觉得我的问题微不足道。
我有一个这样定义的结构:
typedef struct SymTable {
symbol_t *rootNode;
} symTable_t;
像这样的初始化函数:
void initTable(symTable_t *table) {
table->rootNode = NULL;
}
在我的 main.c 中调用函数:
symTable_t *newTable = malloc(sizeof(symTable_t));
// check if malloc was successful
initTable(&newTable);
当我尝试翻译时它抛出这两个错误:
main.c:12:12: error: passing argument 1 of ‘initTable’
from incompatible pointer type [-Werror=incompatible-pointer-types]
initTable(&newTable);
^
In file included from main.c:1:0:
sym_tab.c:18:7: note: expected ‘symTable_t * {aka struct SymTable *}’ but argument is of type ‘symTable_t ** {aka struct SymTable **}’
void initTable(symTable_t *table) {
当我像 _initTable(symTable_t table)_ 一样定义 initTable() 时,它只会抛出更多错误。
提前谢谢你。
您传递的是指向 symTable_t
的指针,而不仅仅是指向 symTable_t
的指针。
去掉这一行的&
:
initTable(&newTable);
错误将消失。