あなたが抱えていた問題の 1 つは、typedef を作成した後、構造体ポインターを宣言する際に構造体を再利用していたことですstruct list *start;
。また、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
どこでも構造体リストを使用することを選択し、typedef を使用して作成をスキップできます。typedef を使用すると、http: //en.wikipedia.org/wiki/Struct_%28C_programming_language%29#typedefに記載されているように、コードの読み取りが簡素化されます。
私はあなたが持っているものを書き直したので、それをコンパイルしてもう少しよく理解し、1 つのノードにいくつかのデータを入れることができました。私が C を学んでいたとき、構造体 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;
}