我正在尝试定义一个将返回指向结构的指针的函数。我认为我正确地遵循了这一点,(返回一个结构指针)但是当我尝试访问指针的成员时,我的代码一直抱怨这个错误消息,“错误:取消引用指向不完整类型的指针”。
这是我的代码
#include <stdio.h>
#include <string.h>
#include <assert.h>
struct lnode
{
char* word;
int line;
int count;
struct lnode* nn; /* nn = next node */
};
struct lnode* newNode (char* word, int line)
{
struct lnode* newn = (struct lnode*) malloc(sizeof (struct lnode));
if (!newn)
return NULL;
strcpy (newn->word, word);
newn->line = line;
newn->count = 1;
newn->nn = NULL;
return newn;
}
int main()
{
char* p = "hello";
struct lnode* head = newNode (p, 5);
//the following lines are causing errors
assert (!strcmp (head->word, p));
assert (head->line == 5);
assert (head->count == 1);
assert (!head->nn);
return 0;
}
谢谢您的帮助!