C语言结构体

c 复制代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
 
//struct Student_s {
//	int num;
//	char name[20];
//	char gender;
//	int age;
//	float Chinese;
//	float Math;
//	float English;
//	char addr[30];
//};
//最后的分号一定要写!!!!!!

//typedef struct Student_s Student_t;
//typedef struct Student_s* pStudent_t;

//下面的结构体定义方式更常用!
typedef struct{
	int num;
	char name[20];
	char gender;
	int age;
	float Chinese;
	float Math;
	float English;
	char addr[30];
} Student_t, *pStudent_t;

int main() {
    //结构体赋值,所有的数据成员都拷贝了一份
	//struct Student_s stu1 = {1001, "Wuyifan", 'M', 30, 75, 70, 100, "Canada"};
	//struct Student_s stu2;
	//stu2 = stu1;
    
    //结构体数组
	Student_t stu[3] = {
		1001,"Wuyifan",'M',30,75,70,100,"Canada",
		1003,"Zhaowei",'F',45,80,85,70,"Anhui",
		1005,"Sunhonglei",'M',50,90,90,60,"Heilongjiang"
	};
	
    //从标准输入读取数据,给数据成员赋值
	//for (int i = 0; i < 3; ++i) {
    //  %c前面加上空格,表示忽略前置空白字符
	//	scanf("%d%s %c%d%f%f%f%s",
	//		&stu[i].num, stu[i].name, &stu[i].gender, &stu[i].age,
	//		&stu[i].Chinese, &stu[i].Math, &stu[i].English, stu[i].addr);
	//}
	
    //结构体指针
	pStudent_t p = stu;
	printf("(*p).num = %d\n", (*p).num);//*比. 优先级更低,所以需要加括号
	// -> 和 (*).等价
	printf("p->num = %d\n", p->num);//和上一个是等价的
	int num = p->num++;
	printf("num = %d, p->num = %d\n", num, p->num);
	num = p++->num;
	printf("num = %d, p->num = %d\n", num, p->num);
	num = ++p->num;
	printf("num = %d, p->num = %d\n", num, p->num);
}
相关推荐
JCBP_13 分钟前
I/O进程5
服务器·c语言·后端·算法
森焱森14 分钟前
单片机领域中哈希表
c语言·人工智能·单片机·嵌入式硬件·算法
又熟了1 小时前
定时器介绍及简单应用
c语言·单片机·嵌入式硬件·msp430
baobao17676408301 小时前
C语言进阶之字符函数和字符串函数
c语言·算法
Kita~Ikuyo1 小时前
基础数学:图论与信息论
python·算法·llm·图论
三次拒绝王俊凯1 小时前
数据结构day05
数据结构
烟锁池塘柳02 小时前
【数学建模】(智能优化算法)粒子群优化算法(PSO)详解与Python实现
算法·数学建模
西岭千秋雪_2 小时前
Sentinel核心算法解析の滑动窗口算法
分布式·算法·spring cloud·微服务·中间件·sentinel
Qiu的博客2 小时前
一文读懂 AI
人工智能·算法·开源
Murphy_lx2 小时前
排序(1)
数据结构·算法·排序算法