您遇到的一个问题是您在创建typedef struct list *start;
后重新使用struct来声明结构指针。另外struct和typedef不能有相同的名字。你得到这个:
cc -Wall test.c -o test
test.c: In function ‘main’:
test.c:13: error: ‘list_t’ undeclared (first use in this function)
test.c:13: error: (Each undeclared identifier is reported only once
test.c:13: error: for each function it appears in.)
test.c:13: error: ‘start’ undeclared (first use in this function)
test.c:13: error: ‘cur’ undeclared (first use in this function)
test.c:13: warning: left-hand operand of comma expression has no effect
test.c:16: error: expected expression before ‘)’ token
你可以选择随处使用struct list并跳过使用typedef。使用typedef简化了代码的读取方式,如下所示:http://en.wikipedia.org/wiki/Struct_%28C_programming_language%29#typedef
我已经重写了你刚才的内容,所以我可以编译它并理解它,因此我可以将一些数据放入一个节点。我记得当我学习C时,整个struct typedef概念花了一点时间沉入其中。所以,不要放弃。
#include <stdio.h>
#include <stdlib.h>
struct list {
int data;
struct list *next;
};
typedef struct list list_t;
int main()
{
list_t *start, *cur;
int i;
start = (list_t *) malloc(sizeof(list_t));
if (NULL != start)
{
cur = start; /* Preserve list head, and assign to cur for list trarversal. */
printf("\nEnter the data : ");
scanf("%d", &i);
cur->data = i;
cur->next = NULL;
cur = start;
while(cur != NULL)
{
printf("%d ", cur->data);
cur = cur->next;
}
}
else
{
printf("Malloc failed. Program ending.");
}
return 0;
}
您没有张贴截图... – ThiefMaster 2012-04-26 12:04:15
@ThiefMaster比邮寄没有(没有代码,没有错误日志)破越好.. – Krishnabhadra 2012-04-26 12:05:13
评论:1.不投的malloc'的返回值( )'; 2.不要使用'sizeof struct'作为它的参数,而是使用'sizeof(* start)'; 3.当你从不使用它时,为什么'typedef struct list * head'? – 2012-04-26 12:08:11