我試圖運行的功能實現與結構在C ... ...這是一個程序,怎麼被初始化:結構變量亙古在C
#include<stdio.h>
#include<conio.h>
struct store
{
char name[20];
float price;
int quantity;
};
struct store update (struct store product, float p, int q);
float mul(struct store stock_value);
main()
{
int inc_q;
float inc_p,value;
struct store item = {"xyz", 10.895 ,10}; //## this is where the problem lies ##
printf("name = %s\n price = %d\n quantity = %d\n\n",item.name,item.price,item.quantity);
printf("enter increment in price(1st) and quantity(2nd) : ");
scanf("%f %d",&inc_p,&inc_q);
item = update(item,inc_p,inc_q);
printf("updated values are\n\n");
printf(" name = %d\n price = %d\n quantity = %d",item.name,item.price,item.quantity);
value = mul(item);
printf("\n\n value = %d",value);
}
struct store update(struct store product, float p, int q)
{
product.price+=p;
product.quantity+=q;
return(product);
}
float mul(struct store stock_value)
{
return(stock_value.price*stock_value.quantity);
}
當我初始化該結構存儲項= {」 xyz「,10.895,10};不被存儲在由部件的值,即本亞特(結構存儲項)線路成員:
item.name應「XYZ」,
項。價格應該10.895,
item.quantity應該是;
但除了的item.name = XYZ其他成員採取自己的垃圾值..我無法理解這個行爲... 我使用devC++(版本5.4.2與mingw)...
我得到的問題,因爲我使用char名稱[20]作爲結構存儲的成員?
有人請有助於消除錯誤在我的代碼..回覆很快
發佈你正在得到的輸出.. –
除了不適當的'printf',請考慮閱讀本文http://stackoverflow.com/questions/330793/how-to-initialize-a-struct-in-ansi-c – Aneri
我不知道你可以指定一個這樣的結構。 – Jiminion