学习C/C++的第十五天 结构体

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

//结构体: 就是程序员自定义的一种 “数据类型”
struct student
{
	char name[16];
	int age;
	char tel[12];
};

struct _class
{
	student rock;
	student martin;
	student zsf;
};

int main(void)
{
	//结构体的初始化
	//方式一    定义的时候初始化所有的属性
	struct student rock = { "***",38,"*********" };
	printf("***的姓名: %s  年龄: %d  电话: %s\n", rock.name, rock.age, rock.tel);

	//方式二    定义的时候我们可以指定初始化的属性   VS/VC 不支持  但 gcc 是支持的
	//student s1 = { .name = "***",.age = 100 };

	//方式三    单独初始化每一个属性
	student s2;
	strcpy_s(s2.name, "***");
	s2.age = 40;
	s2.tel[0] = '\0';

	printf("*** 的姓名: %s  年龄: %d  电话: %s\n", s2.name, s2.age, s2.tel);

	//结构体中含有结构体
	_class c1 = { {"***",38,"********"},{"***",38,"***"},{"***",100,"*********"} };

	printf("***同学的姓名: %s  年龄: %d  电话: %s\n", c1.martin.name, c1.martin.age, c1.martin.tel);

	system("pause");
	return 0;
}
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <iostream>

using namespace std;

struct student
{
	char name[16];
	int age;
};

int main(void)
{
	struct student s1, s2;

	//printf("请输入第一个学生的姓名:\n");
	//scanf_s("%s", s1.name,sizeof(s1.name));
	cout << "请输入第一个学生的姓名:" << endl;
	cin >> s1.name;
	//printf("请输入第一个学生的年龄:\n");
	//scanf_s("%d", &s1.age);
	cout << "请输入第一个学生的年龄:" << endl;
	cin >> s1.age;
/*
	printf("请输入第二个学生的姓名:\n");
	scanf_s("%s", s2.name,sizeof(s2.name));
	printf("请输入第二个学生的年龄:\n");
	scanf_s("%d", &s2.age);
*/
	printf("第一个学生的姓名:%s  年龄: %d\n", s1.name, s1.age);

	//结构体的小秘密    结构体变量之间可以直接赋值
	s2 = s1;
	printf("第二个学生的姓名:%s  年龄: %d\n", s2.name, s2.age);

	system("pause");
	return 0;
}
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <iostream>

using namespace std;

struct student
{
	char name[16];
	int age;
};

int main(void)
{
	struct student s[2];

	//printf("请输入第一个学生的姓名:\n");
	//scanf_s("%s", s[0].name,sizeof(s[0].name));
	cout << "请输入第一个学生的姓名:" << endl;
	cin >> s[0].name;
	//printf("请输入第一个学生的年龄:\n");
	//scanf_s("%d", &s[0].age);
	cout << "请输入第一个学生的年龄:" << endl;
	cin >> s[0].age;

	printf("第一个学生的姓名:%s  年龄: %d\n", s[0].name, s[0].age);

	//结构体的小秘密    结构体变量之间可以直接赋值
	s[1] = s[0];
	memcpy(&s[1], &s[0], sizeof(struct student));
	printf("第二个学生的姓名:%s  年龄: %d\n", s[1].name, s[1].age);

	system("pause");
	return 0;
}
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <iostream>

using namespace std;

struct _friend
{
	char name[32];
	char sex;       //m - 男   f - 女
	int age;
};

int main5(void)
{
	_friend girl = { "***", 'f', 18 };

	_friend *my_girl = &girl;

	printf("***的名字: %s, 性别: %s ,年龄: %d\n", girl.name, girl.sex == 'm' ? "男" : "女", girl.age);

	//通过指针访问结构体变量的成员
	//方式一   直接解引
	printf("***的名字: %s, 性别: %s ,年龄: %d\n", (*my_girl).name, (*my_girl).sex == 'm' ? "男" : "女", (*my_girl).age);

	//方式二   直接使用指针访问  符号  ->
	printf("***的名字: %s, 性别: %s ,年龄: %d\n", my_girl->name, my_girl->sex == 'm' ? "男" : "女", my_girl->age);

	system("pause");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值