谢谢你抽出时间阅读我的问题,我看了几个类似的问题,这些问题在这种情况下似乎没有帮助,尽管它们可能会帮助其他有类似麻烦的人:
Struct as incompatible pointer type in C
Incompatible Types Error with Struct C
我试图在c (-std=c99)中创建一个简单的链接列表结构,我的结构在这一点上是相当通用的:
typedef struct
{
int count;
char* word;
struct node *nextNode;
}node;然后在一个函数中,我有一个“根”或"head“节点:
node *root;
root = (node *) malloc(sizeof(node));我尝试将一个node分配给后面函数中的根节点nextNode,如下所示:
if(root->nextNode == 0)
{
root->nextNode = foo;
}这会导致错误:
"error incompatibles types when assigning to type `struct node*` from type `node`&foo没有改善这种情况,反而导致了lvalue required as unary样式的错误。
以下是围绕我的问题的背景:
#include <stdio.h>
#include <malloc.h>
#include <string.h>
typedef struct
{
int count;
char* word;
struct node *nextNode;
}node;
node makenode(char *word)
{
node x;
x.word = word;
x.count = 1;
return x;
}
void processInput(int threshold, const char* filename)
{
node *root;
root = (node *) malloc(sizeof(node));
root->nextNode = 0;
char* word;
while(fgets(word, 29, stdin) != NULL){
if(root->nextNode == 0)
{
root->nextNode = makenode(word);
}发布于 2014-09-07 07:23:06
问题
typedef struct // make an alias for a structure with no tag name
{
int count;
char* word;
struct node *nextNode; // with a pointer to struct node (which does not exit)
}node; // name the alias node解决方案
typedef struct node // make an alias for a structure with tag name node
{
int count;
char* word;
struct node *nextNode; // with a pointer to struct node (which is this one)
}node; // name the alias node发布于 2020-10-27 18:57:05
试试这段代码
#include <stdio.h>
#include <malloc.h>
#include <string.h>
typedef struct node //should give a name for this
{
int count;
char* word;
struct node *nextNode;
}node;
static node *makenode(char *word) //start with static and returning type is node* because we are storing this output in root->nextNode which is *node pointer
{
node x;
x.word = word;
x.count = 1;
return x;
}
void processInput(int threshold, const char* filename)
{
node *root;
root = (node *) malloc(sizeof(node));
root->nextNode = NULL; //pointer values should initialised to NULL not 0
char* word;
while(fgets(word, 29, stdin) != NULL){
if(root->nextNode == NULL) ////pointer values should initialised to NULL not 0
{
root->nextNode = makenode(word);
}
}https://stackoverflow.com/questions/25707934
复制相似问题