-2

履歴情報を保持するリンク リストを実装しようとしています。

私のノード構造の定義は次のとおりです。

struct HistoryNode {
    int Value;
    struct HistoryNode *Last, *Next;
};

リスト内のヘッド/テール/現在のノードへのポインターを含む別の構造も作成しました。

struct _History {
    struct HistoryNode *Head, *Tail, *Current;
} History = {
    NULL, NULL, NULL,
};

最後に、ノードをリストに追加する関数を作成しました。

void AddHistory(void) {
    struct HistoryNode *NewNode;

    //Allocate new node memory
    NewNode = malloc(sizeof(NewNode));
    if(NewNode == NULL) {
        Die("malloc(%d) failed", sizeof(NewNode));
    }

    //Re-arrange pointers in new node and head/tail/current
    if(History.Current == NULL) {
        NewNode->Next = NULL;
        NewNode->Last = NULL;

        History.Current = NewNode;
        History.Head = NewNode;
        History.Tail = NewNode;
    } else {
        NewNode->Next = NULL;
        NewNode->Last = History.Current;

        History.Current = NewNode;
        History.Tail = NewNode;
    }
}

GCC は、いくつかのエラーとともにこれを吐き出します。

Scribe.c: In function 'AddHistory':
Scribe.c:509:15: error: request for member 'Current' in something not a structure or union
Scribe.c:513:16: error: request for member 'Current' in something not a structure or union
Scribe.c:514:16: error: request for member 'Head' in something not a structure or union
Scribe.c:515:16: error: request for member 'Tail' in something not a structure or union
Scribe.c:518:32: error: request for member 'Current' in something not a structure or union
Scribe.c:520:16: error: request for member 'Current' in something not a structure or union
Scribe.c:521:16: error: request for member 'Tail' in something not a structure or union

なぜこれが起こっているのかわかりません、何か助けはありますか?

ありがとう - アレックス

4

2 に答える 2

1

Historyがグローバル変数として宣言されている場合、 http://cfiddle.net/LwucyBに示されているように、コンパイルは成功しています。質問に関する1つの追加の注意事項は、以下のようにする必要がある割り当てに関連してNewNodeいます

NewNode = malloc(sizeof(struct HistoryNode));

ポインターだけでなく、構造体にもスペースを割り当てる必要があります。

于 2013-04-08T23:56:01.060 に答える
0

NewNode (HistoryNode へのポインター) ではなく、sizeof(HistoryNode) を malloc する必要があります。

于 2013-04-08T23:44:24.000 に答える