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);
}