list.h
#ifndef LIST_H
#define LIST_H
/* Function prototypes */
struct nodeStruct* List_createNode(int item);
#endif
list.c
#include <stdio.h>
#include <stdlib.h>
struct nodeStruct {
int item;
struct nodeStruct *next;
};
struct nodeStruct* List_createNode(int item) {
struct nodeStruct *node = malloc(sizeof(struct nodeStruct));
if (node == NULL) {return NULL;}
node->item = item;
node->next = NULL;
return node;
}
Main.c:
#include "list.h"
#include <assert.h>
#include <sys/types.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
struct nodeStruct *one = List_createNode(1);
while(one != NULL) {
printf("%d", one->item); //error
one= one->next; //error
}
Error: error: dereferencing pointer to incomplete type printf("%d", one->item);
The error is at one->item, I have tried several combinations to dereference, but does not seem to work. What's the right approach?
Updated:
list.h
#ifndef LIST_H
#define LIST_H
struct nodeStruct {
int item;
struct nodeStruct *next;
};
/* Function prototypes */
struct nodeStruct* List_createNode(int item);
#endif
Now the error is, invalid application of ‘sizeof’ to incomplete type ‘struct nodeStruct’
struct nodeStruct *node = malloc(sizeof(struct nodeStruct));
From my list.c file.
struct nodeStructin the main .c file? Also, you probably want to remove the * inone = *one->next;. Oh, and modifyingonemay leak memory. - EOFstruct nodeStructis, in fact, not available to main(). Put the definition ofstruct nodeStructintolist.h. Hint: It's currently inlist.c. - EOF#include "list.h"inlist.c. - EOFint main()function? It's not in themain.cyou've posted - EOF