我试图运行的功能实现与结构在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