私が試したし、可能な限り次のコードを蒸留しました:これは罰金コンパイルC構造体のtypedefのと宣言原因ポインタエラー
#include <stdlib.h>
typedef struct item{
struct item *next;
}item;
void addItem(item *list)
{
item *newItem = list;
while(newItem->next != NULL){
newItem = newItem->next;
}
newItem->next = malloc(sizeof(item));
newItem->next->next = NULL;
}
int main()
{
item *groceryList = NULL;
groceryList = malloc(sizeof(item));
if(groceryList == NULL)
return 1;
groceryList->next = NULL;
addItem(groceryList);
return 0;
}
。しかし、(変更の任意の組み合わせまたは)への構造体宣言を変更:
structpointertest.c:11:11: warning: assignment from incompatible pointer type structpointertest.c:15:15: error: request for member 'next' in something not a structure or union
を、私はこの問題を引き起こしている構造体宣言では理解していない:
typedef struct{ /*Removed "item"*/
item *next; /*Removed "struct"*/
}item;
は、コンパイル時に以下のエラーが発生します?それはネストされた構造体を使用しているという事実と関係がありますか?
ありがとうございました。
コンパイラは 'item'が宣言行' item * next'にあるかどうかを知りません。 –
「入れ子構造」はありません。そして、無限の再帰なしにすることはできません。 – Olaf
申し訳ありませんが、私はひどく言いました。私は構造の入れ子を意味するのではなく、エイリアスの入れ子を意味しました。 –