一、定义结构体变量:
知识点:
1、只有结构体变量才分配地址,而结构体的定义是不分配空间的。**
2、结构体中各成员的定义和之前的变量定义一样,但在定义时也不分配空间。
3、结构体变量的声明需要在主函数之上或者主函数中声明,如果在主函数之下则会报错
4、c语言中的结构体不能直接进行强制转换,只有结构体指针才能进行强制转换
5、相同类型的成员是可以定义在同一类型下的
struct Student
{
int number,age;//int型学号和年龄
char name[20],sex;//char类型姓名和性别
float score;
};
最后的分号不要忘了 !
1、使用typedef
#include <stdio.h>
#include <string.h>
//typedef struct Student { //此处写不写Student都可以
typedef struct {
char name[100];
char sex;
float Math_Score;
float Eng_Score;
}STU;
int main() {
STU stu1;
stu1 = { "XH",'F',98.0,97.0 };
printf("%s\t%c\t%f\t%f\n",stu1.name,stu1.sex,stu1.Math_Score,stu1.Eng_Score);
return 0;
}
运行结果:
二、结构体指针:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct Student { //此处写不写Student都可以
//typedef struct {
char name[100];
char sex;
float Math_Score;
float Eng_Score;
}STU;
int main() {
STU stu1;
STU *p;
p = &stu1;
stu1 = { "XH",'F',98.0,97.0 };
printf("\t常规方法输出结构体变量内容\t\n");
printf("%s\t%c\t%f\t%f\n",stu1.name,stu1.sex,stu1.Math_Score,stu1.Eng_Score);
printf("\t指针方法输出结构体变量内容\t\n");
printf("%s\t%c\t%f\t%f\n", p->name, p->sex, p->Math_Score,
p->Eng_Score);
return 0;
}
运行结果:
结构体数组的指针
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct Student { //此处写不写Student都可以
//typedef struct {
char name[100];
char sex;
float Math_Score;
float Eng_Score;
}STU;
int main() {
STU stu1[2]={ { "XH",'F',98.0,97.0 },{"GD",'M',89.0,90.8} };;
STU *p;
p = stu1;
int i;
printf("\t常规方法输出结构体变量内容\t\n");
for (i = 0; i < 2; i++) {
printf("%s\t%c\t%f\t%f\n", stu1[i].name, stu1[i].sex, stu1[i].Math_Score, stu1[i].Eng_Score);
}
printf("\t指针方法输出结构体变量内容\t\n");
for (i = 0; i < 2; i++) {
printf("%s\t%c\t%f\t%f\n", (p+i)->name, (p + i)->sex, (p + i)->Math_Score,
(p + i)->Eng_Score);
}
return 0;
}
运行结果:
三、结构体含指针成员变量
1、定义并初始化结构体数组:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
typedef struct Student { //此处写不写Student都可以
//typedef struct {
char name[100];
char sex;
float Math_Score;
float Eng_Score;
char *num; //学号
}STU;
int main()
{
STU stu1[2] = { { "XH",'F',98.0,97.0,"a" },{"GD",'M',89.0,90.8,"b"} };
//注意char *num ,在结构体成员里面为 "a" 和 "b"
for (int i = 0; i < 2; i++)
{
printf("stu: %s %c %f %f %s \n", stu1[i].name, stu1[i].sex, stu1[i].Math_Score, stu1[i].Eng_Score, stu1[i].num);
}
return 0;
}
同时,文件后缀改为.c,而不是.cpp
运行结果:
或者是:
中阶C语言 结构体(typedef用法、多维结构体、指针、内嵌函数、赋值)
四、结构体含指针成员,结构体指针变量,初始化及地址
#include "stdio.h"
#include "string.h"
#include <malloc.h>
#include <stdlib.h>
typedef struct student
{
char *name;
int score;
//struct student *next;
}Stu;
static int q;
int main()
{
Stu s1;
s1.name = (char *)malloc(sizeof(char));
strcpy(s1.name, "Jimy");
s1.score = 99;
printf("%s\t%d\n", s1.name, s1.score);
Stu s2 = { (char *)"XH",80 };
printf("%s\t%d\n", s2.name, s2.score);
Stu s3;
s3.name = (char *)malloc(sizeof(char));
strcpy(s3.name, "GD");
s3.score = 59;
printf("%s\t%d\n", s3.name, s3.score);
Stu *s4;
s4 = (Stu *)malloc(sizeof(Stu));
s4->name = (char *)malloc(sizeof(char));
strcpy(s4->name, "WH");
s4->score = 36;
printf("%s\t%d\n", s4->name, s4->score);
//输出 结构体变量 s1 s2 的地址
printf("s1地址为 %p\n", s1);
printf("s2地址为 %p\n", s2);
printf("s3地址为 %p\n", s3);
printf("结构体指针s4地址为 %p\n", s4);
printf("输出全局静态变量的地址:%p\n",&q);
return 0;
}
运行结果:
参考:C语言结构体,结构体数组,以及结构体指针的初始化和定义
五、枚举
枚举是 C 语言中的一种基本数据类型,它可以让数据更简洁,更易读。
枚举语法定义格式为:
enum 枚举名 {枚举元素1,枚举元素2,……};
示例:
enum DAY
{
MON=1, TUE, WED, THU, FRI, SAT, SUN
};
可以在定义枚举类型时改变枚举元素的值:
enum season {spring, summer=3, autumn, winter};
没有指定值的枚举元素,其值为前一元素加 1。也就说 spring 的值为 0,summer 的值为 3,autumn 的值为 4,winter 的值为 5
注意:第一个枚举成员的默认值为整型的 0,后续枚举成员的值在前一个成员上加 1。我们在这个实例中把第一个枚举成员的值定义为 1,第二个就为 2,以此类推。
枚举变量的定义:
1、先定义枚举类型,再定义枚举变量
enum DAY
{
MON=1, TUE, WED, THU, FRI, SAT, SUN
};
enum DAY day;
2、定义枚举类型的同时定义枚举变量
enum DAY
{
MON=1, TUE, WED, THU, FRI, SAT, SUN
} day;
3、省略枚举名称,直接定义枚举变量
enum
{
MON=1, TUE, WED, THU, FRI, SAT, SUN
} day;
示例代码:
#include<stdio.h>
enum DAY
{
MON = 1, TUE, WED, THU, FRI, SAT, SUN
};
int main()
{
enum DAY day;
day = WED;
printf("%d", day);
return 0;
}
运行结果: