我正在自己编写一个 shell,并且对于以下代码(仅是重要的行),我总是收到此警告“来自不兼容的指针类型的分配”
if (first_struct == NULL)
{
first_struct = &parg;
}
else
{
Pargs** temp = first_struct;
while ((*temp)->next != NULL)
temp = &((*temp)->next);<--incompatible pointer type
(*temp)->next = parg;<--incompatible pointer type
}
parg->next = NULL;
对我来说,只有相同的指针类型。
这是该结构的代码:
typedef struct
{
struct Pargs* next;
char* command;
char* args[11];
} Pargs;
该结构在上面几行声明:
Pargs* parg = malloc(sizeof (Pargs));
也许我没有看到自己的失败,但我几个小时以来一直在查看这些行,我不知道为什么这不正确。
PS:如果有人需要更多代码或信息,请随时询问,我还有更多;)
最佳答案
typedef struct
{
struct Pargs* next;
char* command;
char* args[11];
} Pargs;
您在结构本身内引用 typedef Pargs
。这不起作用,因为它是指向不完整类型的指针,并且在给出完整定义之前您无法引用它。
你可以这样做 -
typedef struct name // give any desired name to structure
{
struct name *next; //use struct's name to declare pointer next
char* command;
char* args[11];
} Pargs;
关于c - 为什么此代码会产生 "assignment from incompatible pointertype"警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33652201/