1

以下のコードがあります。としてエラーが発生してい'list' undeclared (first use in this function)ます。私を助けてください

#include <stdio.h>
#include <stdlib.h>
struct list{
int data;
struct list *next;
};
typedef struct list *head;

int main()
{
    struct list *start;
    int i;

    start = (list *) malloc(sizeof(struct list));
    printf("\nEnter the data : \n");
    scanf("%d", &i);
    start->data = i;
    start->next = NULL;
    while(list->next != NULL)
    {
        printf("%d ", list->data);
        list = list->next;
    }

    return 0;
}
4

7 に答える 7

3

listvariable name の代わりにtype を使用していますstart。適切なコード:

while (start->next != NULL)
{
    start = start->next;
    // etc.
}
于 2012-04-26T12:05:48.907 に答える
3

の戻り値の型をキャストしないでくださいmalloc-それには何のメリットもありません。この場合は間違っています!

start = (list *) malloc(sizeof(struct list));

する必要があります

start = malloc(sizeof(struct list));

タイプlist *が存在しません。あなたが意味しstruct list *た。

書くことでさらに安全にすることができます

start = malloc(sizeof(*start));

このようにmallocして、 の (ポインター) 型に十分なバイトが自動的に確保されます。これは、後で の型を変更するstartときに役立ちます。呼び出しは少しも変わりません。startmalloc

于 2012-04-26T12:07:57.280 に答える
1

あなたの変数は « start » と名付けられ、あなたはそれを « list » と呼びました。

于 2012-04-26T12:23:01.050 に答える
1
#include <stdio.h>
#include <stdlib.h>
typedef struct list{
    int data;
    struct list *next;
} list;

typedef struct list *head;

int main()
{
    struct list *start;
    int i;

    start = (list *) malloc(sizeof(struct list));
    printf("\nEnter the data : \n");
    scanf("%d", &i);
    start->data = i;
    start->next = NULL;
    while(start->next != NULL)
    {
        start = start->next;
    }

    return 0;
}

タイプを定義できます(リスト*)

于 2012-04-26T12:10:00.463 に答える
1

start = (list *) malloc(sizeof(struct list));

不要な型キャストが含まれています。やるだけ

start = malloc(sizeof(struct list));

ただし、コードにはこれよりも多くの問題があります。私自身の質問をすることで、あなたの質問に最もよく答えることができます: あなたの心の中でlist、タイプまたはオブジェクトですか?

この質問に答えると、コードを修正できる可能性があります。幸運を。

于 2012-04-26T12:09:18.933 に答える
0

あなたが抱えていた問題の 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;
}
于 2012-04-26T12:34:36.770 に答える
0
start = (struct list *) malloc ...

キャストで構造体を見逃しました。アンタレスが指摘したように、これはまったく必要ありません。

start = malloc ...
于 2012-04-26T12:08:23.130 に答える