首页 > 学院 > 开发设计 > 正文

函数和结构体

2019-11-08 01:06:06
字体:
来源:转载
供稿:网友

* 传递和返回结构、传递结构的地址*

#include <iostream>using namespace std;struct order { char *order_num; char *user_name; string address;};/** * 当结构比较小时使用值传递 */void PRint_order(const order order) { cout << "order_num = " << order.order_num << endl; cout << "user_name = " << order.user_name << endl; cout << "address = " << order.address << endl;}/** * 传递地址,由于函数不修改结构内容,使用const修饰符 */void print_order(const order *order) { cout << "order_num = " << order->order_num << endl; cout << "user_name = " << order->user_name << endl; cout << "address = " << order->address << endl;}/** * 返回结构 */order build_order(char *order_num, char *user_name, string address) { order ord; ord.order_num = order_num; ord.user_name = user_name; ord.address = address; return ord;}int main() { //测试结构传递 order o1 = { "020134242", "user_1", "chengDu" }; print_order(o1); print_order(&o1); //测试返回结构 order o2 = build_order("123", "wyl", "chengDu"); print_order(o2); return 0;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表