Thanks for taking the time to read my question, I've looked at a few similar questions and they don't seem to help in this instance although may help others with similar troubles:
C: Incompatible types?
Struct as incompatible pointer type in C
Incompatible Types Error with Struct C
I'm attempting to create a simple linked list structure in c (-std=c99), my structure is fairly generic at this point:
typedef struct
{
int count;
char* word;
struct node *nextNode;
}node;
then in a function i have a "root" or "head" node:
node *root;
root = (node *) malloc(sizeof(node));
and i attempt to assign a node
to the root nodes nextNode
later on in the function like so:
if(root->nextNode == 0)
{
root->nextNode = foo;
}
Which leads to an error:
"error incompatibles types when assigning to type struct node*
from type node
&foo
does not improve the situation, instead resulting in a lvalue required as unary
style error.
Here is the context surrounding my issue:
#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);
}
Problem
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
Solution
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