2016-11-05 55 views
0
#include <stdio.h> 
#include "InventoryManager.h" 

void displayInventory(const struct Item items[], const int size) 
{ 
printf("\n\n"); 
printf("Inventory\n"); 
printf("=========================================\n"); 
printf("Sku   Price  Quanity\n"); 
int index = 0; 
for (index = 0; index < size; index++) 
{ 
    printf("%-10.0d %-10.2f %-10d\n", items[index].sku, items[index].price, items[index].quantity); 
} 
printf("=========================================\n"); 
} 

當我嘗試訪問數組內的結構值時,我在「項目」下出現紅色下劃線。如何訪問按值傳遞給函數的結構數組中的成員?

我有3個文件,inventoryManger.h,inventoryManager.c,shopping_lab_2.c ...名爲Item的結構體在shopping_lab_2.c中創建,並且您在堆棧溢出中看到的函數在inventoryManager.c中生成。

+0

貌似的'結構Item'的定義是不存在的。它是在這個文件還是這個文件包含的文件? –

+0

我有3個文件,inventoryManger.h,inventoryManager.c,shopping_lab_2.c ...名爲Item的結構是在shopping_lab_2.c中創建的,並且您在堆棧溢出中看到的函數在inventoryManager.c中生成。 – user3134679

+0

您需要在其使用的任何文件中具有結構定義。如果它在多個.c文件中使用,則應將該定義放在.h文件中,並讓.c文件包含它。 –

回答

0

我不知道你怎麼稱呼你的功能。下面的程序工作沒有錯誤或警告:

struct Item{ 
int sku; 
float price; 
int quantity; 
}; 

void displayInventory(const struct Item items[], const int size) 
{ 
printf("\n\n"); 
printf("Inventory\n"); 
printf("=========================================\n"); 
printf("Sku   Price  Quanity\n"); 
int index = 0; 
for (index = 0; index < size; index++) 
{ 
    printf("%-10.0d %-10.2f %-10d\n", items[index].sku, items[index].price, items[index].quantity); 
} 
printf("=========================================\n"); 
} 

int main() 
{ 
Item items[2] = {1, 1.1, 10, 2, 2.2, 20 }; // initialization 
displayInventory(items, 2); 
return 0; 
} 

輸出:

Inventory 
========================================= 
Sku   Price  Quanity 
1   1.10  10   
2   2.20  20   
========================================= 
相關問題