您的位置:首页 > 其它

结构体的嵌套与返回

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: