将整个结构体作为一个参数传递给函数

来源:互联网 发布:单片机电子琴原理图 编辑:程序博客网 时间:2024/06/10 00:01
//程序:整个结构体作为一个参数传递给函数(参考:C程序设计第四版第307页)
struct stores{
 char name[20];
 float price;
 int quantity;};  // 声明一个结构体
struct stores update(struct stores product,float p,int q);  //函数声明,更改结构体
float mul(struct stores stock);  //函数声明,返回价格和数量的乘积
main(){
 float p_increment,value;  //value为乘积的结果
 int q_increment;
 struct stores item={"XYZ",25.75,12};  //初始化 结构体item
 printf("\nInput increment values:");  
 printf("  price increment and quantity increment\n");
 scanf("%f %d",&p_increment,&q_increment);  //输入增加的值
 item=update(item, p_increment,  q_increment);  //调用函数,传递结构体
 printf("update values of item\n\n");
 printf("name     :%s\n",item.name);   //输出更新后的值
 printf("price    :%f\n",item.price);
 printf("quantity :%d\n",item.quantity);
 value=mul(item);  //调用函数,传递结构体,返回乘积结果
 printf("\nValues of the item=%f\n",value);
}
struct stores update(struct stores product ,float p,int q) {   //函数定义
 product.price+=p;
 product.quantity+=q;
 return(product);   //返回结构体
}
float mul(struct stores stock) { //函数定义
 return(stock.price*stock.quantity);
}
//输入10 12, 查看结果