将整个结构体作为一个参数传递给函数
来源:互联网 发布:单片机电子琴原理图 编辑:程序博客网 时间: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, 查看结果
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, 查看结果
- 将整个结构体作为一个参数传递给函数
- 将对象作为参数传递给成员函数
- 函数指针作为参数传递给函数
- 关于函数作为参数传递给函数
- 结构体作为参数传递
- 整个数组作为参数传递
- 可以通过定义js函数,将jsp变量值作为参数传递给该函数
- Matlab中如何将(自定义)函数作为参数传递给另一个函数
- Matlab中如何将(自定义)函数作为参数传递给另一个函数
- 关于将函数的地址作为参数传递给另一个函数调用
- 将函数的地址作为参数传递给另一个函数调用
- go语言将函数作为参数传递
- JS 将函数作为参数传递调用
- 将函数名(地址)作为参数传递
- lua 将函数作为参数传递
- 将一个结构体变量中的数据传递给另一个函数
- 关于将数组作为参数传递给函数的简单使用
- 匿名方法---将代码块作为参数传递给委托
- Django URL 解析
- 进程间互斥 Mutex
- 高斯混合模型和EM算法
- TopCoder SRM 595 Div2 第3题
- MySQL时间戳转换时间
- 将整个结构体作为一个参数传递给函数
- python------unicode字符串转换为其他类型
- 《hadoop权威指南》学习笔记-MapReduce应用开发(上)
- Imagema给图片添加水印,水印的图片会变色的原因:图片的色彩空间不对应,被水印图的colorspace是CMYK
- MySQL数据备份与恢复
- [VC++.NET编程]int转化成String
- 规则化和模型选择
- 调试
- 单例模式