における構造体の構造体を印刷:私はこのような構造体を持っているリンクリスト
typedef struct stockItem {
char *componentType;
char *stockCode;
int numOfItems;
int price;
} stockItem;
// declaration
stockItem *stockItem_new(char *componentType, char *stockCode, int numOfItems, int price);
そして、多くの在庫のアイテムを格納するために、このような構造体(リンクリスト)
typedef struct inventory {
struct stockItem item;
struct inventory *next;
}inventory;
これらされています両方とも異なるヘッダーファイルにあります。
私はリンクリストを作成している、私はデータの特定のビットをオフに印刷したい、など:
void outputData(){
// This temporarily takes the location of the structs in the
// linked list as we cycle through them to the end
struct inventory *myInv = pFirstNode;
printf("Current Inventory\n\n");
// Until the ptr reaches a value of NULL for next we'll
// keep printing out values
while(myInv != NULL){
// HERE IS MY PROBLEM HOW DO I PRINT OFF THE COMPONENTTYPE FROM THIS
printf("%s\n\n", myInv->item->compnentType);
// Switch to the next struct in the list
myInv = myInv->next;
}
}
EDIT:
stockItem *stockItem_new(char *componentType, char *stockCode, int numOfItems, int price){
// creates a new duration for the song
stockItem *item = (stockItem*)malloc(sizeof(stockItem));
// assigns the attributes
item->componentType = componentType;
item->stockCode = stockCode;
item->numOfItems = numOfItems;
item->price = price;
// returns it
return item;
}
我々はCでmalloc' 'の結果をキャストしないでくださいstockItem_new' –
Certaininly @AlterMann –
'のコードを参照してくださいする必要があり、また、なぜあなたは「'(ポインタを返します)stockItem_new'あなたはドン場合はありますか'inventory'にポインタを置いていますか? (その宣言で 'item'は' stockItem'へのポインタではないので 'item.compnentType'となります)。 – crashmstr