结构体的嵌套与返回
2017-11-15 17:28
267 查看
结构体的嵌套
#include "stdafx.h" struct birthday { int year; int month; int day; }; //为了避免以后其他结构体需要用到 birthday 需要将结构体提出来定义 typedef struct stu { char name[50]; char sex; int age; float score; //struct birthday //{ // int year; // int month; // int day; //}birth; struct birthday birth; //类型申明完之后一定要申明 birth这个变量 }Stu; struct teacher { char id[100]; char name[200]; struct birthday birth; }; int _tmain(int argc, _TCHAR* argv[]) { Stu ss = { "zhansan", 'x', 23, 89, { 1990, 9, 9 } }; ss.birth.year = 1999; printf("year = %d month = %d day = %d\n", ss.birth.year, ss.birth.month, ss.birth.day); return 0; }
返回结构体(复数的运算)
#include "stdafx.h" //结构体做参数和返回值 传指针作参数,不管结构体多大, //只传4个字节的指针更有效率 struct Complex { float real; float image; }; struct Complex add(struct Complex x, struct Complex y) { struct Complex ret; ret.real = x.real + y.real; ret.image = x.image + y.image; return ret; } int _tmain(int argc, _TCHAR* argv[]) { struct Complex a = { 3.2, 3.4 }; struct Complex b = { 1.2, 1.1 }; struct Complex c = add(a, b); printf("c.real=%.2f c.image=%.2f\n", c.real, c.image); return 0; }
相关文章推荐
- C#调用C++方法,C#使用c++方法返回类或结构体
- 结构体下嵌套结构体的小例子
- rn_xtcxyczjh-6 并发[线程2 头文件重复包含 函数返回值的统一 结构体中的变长数组 线程锁接口]
- C#调用C++方法,C#使用c++方法返回类或结构体
- 48,结构体嵌套
- 结构体嵌套、结构体数组、排序
- 结构体1(嵌套使用)
- 结构体嵌套二级指针
- 一步一步重写 CodeIgniter 框架 (8) —— 视图的嵌套输出与返回
- 6_8_4修改下面的结构体,使之包含一个返回订单总价格的函数.
- golang基础-接口、接口嵌套、类型断言、接口与结构体_接口等转换
- Java—读取多层嵌套Json文件的内容并根据索引返回JsonObject
- 循环里面嵌套一个异步请求,所有循环执行完毕后再返回请求的数据
- 结构体嵌套代码举例
- 结构体嵌套结构体名
- 返回结构体指针
- 结构体嵌套
- Mybatis——返回类型为 集合嵌套集合 应该如何处理
- C++ 结构体嵌套、空结构体和空类
- 结构体的嵌套