2016-03-28 16 views
1

Böyle bir yapı vardır:bağlı bir listedeki bir yapı içinde bir yapı yazdırma

typedef struct stockItem { 
    char *componentType; 
    char *stockCode; 
    int numOfItems; 
    int price; 
} stockItem; 

// declaration 
stockItem *stockItem_new(char *componentType, char *stockCode, int numOfItems, int price); 

Ve böyle bir yapı birçok stok öğeleri (bağlantılı liste) Bunlar

typedef struct inventory { 
    struct stockItem item; 
    struct inventory *next; 
}inventory; 

saklamak için her ikisi de farklı başlık dosyasında. Ben bağlantılı liste oluşturduk

, I, belirli veri bitlerini kapalı yazdırmak böyle istiyorum:

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; 
    } 
} 

DÜZENLEME:

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; 
} 
+0

"stockItem_new" kodunu görmemiz gerekiyor. –

+0

Certaininly @AlterMann –

+0

"malloc" in sonucunu C'ye yazmayın, ayrıca neden "stockItem_new" (işaretçiyi döndürür) varsa "Envanter" de bir işaretçi tutmak mı? (Bu beyan ile, 'item'' stockItem' için bir işaretçi olmadığı için 'item.compnentType' olacaktır. – crashmstr

cevap

1

Biz kodun geri kalanını görmüyorum, Eğer bir işaretçi dönen stockItem_new beri ama, o zaman bu yanlıştır: yerine

typedef struct inventory { 
    struct stockItem item; ///missing *! 
    struct inventory *next; 
} inventory; 

biz ilan etmek gerek o kadar:

typedef struct inventory { 
    struct stockItem *item; 
    struct inventory *next; 
} inventory; 

Sonra stockItem_new fonksiyonu ile item atayabilirsiniz ve beklediğiniz gibi outputData çalışacaktır.

Güncelleme: stockItem_new yılında , sen componentType içeriğinin bir kopyasını yaparak, ama sadece aynı değere işaret değildir. Sen componentType içeriğini yeterli bellek ve kopyalar yeni tamponu da her zaman ayrılacağı ve stockItem_new geçmesine veya strdup

item->componentType = strdup(componentType); 

Bu o bakmak gerek ya. Yapınızda tuttuğunuz tüm dizeler için bunu yapmanız gerekir (sadece adres kopyalandığından!).

+0

'u kabul ediyorum. Cevabınız için teşekkürler, başka bir hızlı soru Verileri yazdırıyorum ve bazı nedenlerden dolayı stoktaki her bir ürün için aynı stok kodunu alıyorum ama ints iyi çalışıyor (fiyat, sayım) herhangi bir fikrin var mı? işaretçiler –

+0

Her seferinde aynı arabelleğe işaret ediyorsunuz. "stockItem_new" öğesinde, [strdup] 'ı (http://pubs.opengroup.org/onlinepubs/009695399/functions/strdup.html) yeni depolamaya ayırmak için kullanabilirsiniz. dize kopyası – crashmstr

+0

Ben bunu stockItem * ile yapmıyorum = malloc (sizeof (stockItem)); –

İlgili konular