-3

ファイルからレコードを読み取り、リンクされたリストに入れるための単純なプログラムを純粋な C で作成しました。グローバル変数の使用は許可されていません。プログラムは次のようになります。

Here are some includes
Some #defines

Function headers for list manipulation:
 void add_item(Record * p, LL * ll);
 void print_all(LL * ll);
 void load(LL * ll);
 ...

    int main(int argc, char const *argv[])
    {

        // Sample struct defining one record
        typedef struct Record
        {
            char sign[10];
            long int isbn;
            char name[100];
            char authors[100];
            long int date;
            int id;
            struct Record * next;
        } Record;

        // Information about linked list (LL)
        typedef struct LL
        {
            Record * HEAD;
        }LL;

        // create new Linked List
        LL * ll = (LL *) malloc(sizeof(LL));

        // init 
        ll->HEAD = NULL;

        // Some other work with ll, record ...
}

// now functions its self

// add item p into ll
void add_item(Record * p, LL * ll)
{
    if (ll->HEAD == NULL)
    {
        ll->HEAD = p;
        ll->HEAD->next = NULL;
    }
    else
    {
        Record * cur = ll->HEAD;

        while(cur->next != NULL)
            cur = cur->next;
        cur->next = p;
    }
}

void print_all(LL * ll)
{
    if (!ll->HEAD)
    {
        printf("%s\n", "ll->HEAD is NULL...");
        return;
    }

    Record * cur = ll->HEAD;

    while(cur)
    {
        printf("%s\n", cur->name );
        cur = cur->next;
    }
}

// other functions

Ubuntu 12.04 で gcc を使用してコンパイルすると、次のようになります。

project.c:20:15: error: unknown type name ‘Record’
project.c:20:27: error: unknown type name ‘LL’
project.c:21:16: error: unknown type name ‘LL’
project.c:22:11: error: unknown type name ‘LL’
project.c:145:15: error: unknown type name ‘Record’
project.c:145:27: error: unknown type name ‘LL’
project.c:162:16: error: unknown type name ‘LL’
project.c:182:11: error: unknown type name ‘LL’

構造体がそれ自身で main() で宣言されている場合、関数ヘッダーの前にコンパイラーに知らせるにはどうすればよいstruct Recordですか?struct LL

4

1 に答える 1

4

できません。

宣言は、関数が宣言されているのと同じレベルから見える必要があります。

リスト関数プロトタイプの前に構造体宣言を移動します。

または、事前宣言を追加します。

typedef struct Record Record;

リスト関数がポインター ( ) のみを処理している限り、Record *それは機能するはずです。

于 2013-05-02T14:08:49.500 に答える