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);
}
相关推荐
如竟没有火炬25 分钟前
全排列——交换的思想
开发语言·数据结构·python·算法·leetcode·深度优先
寂静山林38 分钟前
UVa 12526 Cellphone Typing
算法
czy87874751 小时前
用C语言实现原型模式
c语言·原型模式
czy87874752 小时前
用C语言实现原型模式时,如何确定需要深拷贝还是浅拷贝?
c语言·原型模式
kyle~2 小时前
C++---嵌套类型(Nested Types)封装与泛型的基石
开发语言·c++·算法
sali-tec2 小时前
C# 基于halcon的视觉工作流-章48-短路断路
开发语言·图像处理·人工智能·算法·计算机视觉
墨染点香2 小时前
LeetCode 刷题【128. 最长连续序列】
算法·leetcode·职场和发展
被AI抢饭碗的人2 小时前
算法题(240):最大食物链计数
算法
熬了夜的程序员2 小时前
【LeetCode】82. 删除排序链表中的重复元素 II
数据结构·算法·leetcode·链表·职场和发展·矩阵·深度优先
欧克小奥2 小时前
Floyd判圈算法(Floyd Cycle Detection Algorithm)
算法·floyd