主页 > 互联网  > 

C语言基础(五)——结构体与C++引用


七、结构体与C++引用 7.1 结构体的定义、初始化、结构体数组

C 语言提供结构体来管理不同类型的数据组合。通过将不同类型的数据组合成一个整体,方便引用 例如,一名学生有学号、姓 名、性别、年龄、地址等属性,如果针对学生的学号、姓名、年龄等都单独定义一个变量,那么在有多名学生时,变量就难以分清。就可通过结构体来管理

声明一个结构体类型的一般形式为

struct 结构体名 {成员表列}; struct student { int num;char name[20];char sex; int age;float score;char addr[30]; };

先声明结构体类型,再定义变量名。

struct student student1,student2;

【例1】结构体的scanf读取和输出

#include <stdio.h> struct student{ int num; char name[20]; char sex; int age; float score; char addr[30]; };//结构体类型声明,注意最后一定要加分号 int main() { struct student s={1001,"lele",'M',20,85.4,"Shenzhen"}; struct student sarr[3];//定义一个结构体数组变量 int i; //结构体输出必须单独去访问内部的每个成员 s.num=1003; //单独赋值 输出的值会改变 printf("%d %s %c %d %f %s\n",s.num,s.name,s.sex,s.age,s.score,s.addr); printf("--------------------------------------\n"); // scanf("%d%s %c%d%f%s",&s.num,s.name,&s.sex,&s.age,&s.score,s.addr); for(i=0;i<3;i++) { scanf("%d%s %c%d%f%s",&sarr[i].num,sarr[i].name,&sarr[i].sex,&sarr[i].age,&sarr[i].score,sarr[i].addr); } for(i=0;i<3;i++)//结构体数组的输出 { printf("%d %s %c %d %f %s\n",sarr[i].num,sarr[i].name,sarr[i].sex,sarr[i].age,sarr[i].score,sarr[i].addr); } return 0; }

结构体类型声明要放在 main 函数之前,这样 main 函数中才可以使用这个结构体,工作中往往把结构体声明放在头文件中。注意,结构体类型声明最后一定要加分号,否则会编译不通。 定义结构体变量时,使用 struct student 来定义,不能只有 struct 或 student,否则也会编译不通。 结构体的初始化只能在一开始定义,如果 struct student s={1001,"lele",'M',20,85.4,"Shenzhen"}已经执行,即 struct student s 已经定义,就不能再执 行 s={1001,"lele",'M',20,85.4,"Shenzhen"}整型数据(%d)、浮点型数据(%f)、字符串型数据(%s)都会忽略空格,但字符型数据(%c)不会忽略空格,所以读取字符型数据就要在待读取的字符数据与其他数据之间加入空格。

7.2 结构体对齐

结构体本身的对齐规则,考研初试只需要记住,结构体的大小必须是其最大成员的整数倍

结构体对齐,是为了cpu高效的去取内存中上的数据

#include <stdio.h> struct student_type1{ double score;//double是一种浮点类型,8个字节,浮点分为float和double,float占4个字节,记住有这两种即可 short age;//short 是整型,占2个字节 }; struct student_type2{ double score; int height;//如果两个小存储之和是小于最大长度8,那么它们就结合在一起 short age; }; struct student_type3{ int height; //4个字节 char sex; short age; }; //结构体对齐 int main() { struct student_type1 s1; struct student_type2 s2={1,2,3}; struct student_type3 s3; printf("s1 size=%d\n",sizeof(s1)); printf("s2 size=%d\n",sizeof(s2)); printf("s3 size=%d\n",sizeof(s3)); return 0; }

注意:两个小存储挨在一起且和小于最大长度,那两个小存储就结合在一起看 

s1 size=16   //double8 + short 8 s2 size=16   //double>int+short 所以长度为 double8 + (int+short)8 s3 size=8     //int>char+short 所以长度为 int 4 + (char+short)4

7.3 结构体指针与typedef的使用 7.3.1 结构体指针

一个结构体变量的指针就是该变量所占据的内存段的起始地址。可以设置一个指针变量,用它指向一个结构体变量,此时该指针变量的值是结构体变量的起始地址。指针变量也可以用来指向结构体数组中的元素,从而能够通过结构体指针快速访问结构体内的每个成员。

#include <stdio.h> struct student{ int num; char name[20]; char sex; }; //结构体指针的练习 int main() { struct student s={1001,"wangle",'M'}; struct student sarr[3]={1001,"lilei",'M',1005,"zhangsan",'M',1007,"lili",'F'}; struct student *p;//定义了一个结构体指针变量 p=&s; //*p要加括号去访问元素是因为.的优先级比*高,如果不加括号就会报错 printf("%d %s %c\n",(*p).num,(*p).name,(*p).sex);//方式1访问通过结构体指针去访问成员 printf("%d %s %c\n",p->num,p->name,p->sex);//方式2访问通过结构体指针去访问成员,用这种 p=sarr; printf("%d %s %c\n",(*p).num,(*p).name,(*p).sex);//方式1访问通过结构体指针去访问成员 printf("%d %s %c\n",p->num,p->name,p->sex);//方式2访问通过结构体指针去访问成员,用这种 printf("------------------------------\n"); p=p+1; printf("%d %s %c\n",(*p).num,(*p).name,(*p).sex);//方式1访问通过结构体指针去访问成员 printf("%d %s %c\n",p->num,p->name,p->sex);//方式2访问通过结构体指针去访问成员,用这种 return 0; }

由上面例子可以看到,p 就是一个结构体指针,可以对结构体 s 取地址并赋给 p,这样借助成员选择操作符,就可以通过 p 访问结构体的每个成员,然后进行打印。 由于数组名中存储的是数据的首地址,所以可以将 sarr 赋给 p,这样也可以访问对应的成员。 使用(*p).num 访问成员要加括号是因为“.”成员选择的优先级高于“*”(即 取值)运算符,所以必须加括号,通过*p 得到 sarr[0],然后获取对应的成员。 

7.3.2 结构体的使用

定义结构体变量时使用的语句是 struct student s,这样定义结构体变量有些麻烦,每次都需要写 struct student。可以选择使用 typedef 声明新的类型名来代替已有的类型名

#include <stdio.h> //stu 等价于 struct student,pstu等价于struct student* typedef struct student{ int num; char name[20]; char sex; }stu,*pstu; typedef int INGETER;//特定地方使用 //typedef的使用,typedef起别名 int main() { stu s={1001,"wangle",'M'}; stu *p=&s;//定义了一个结构体指针变量 pstu p1=&s;//定义了一个结构体指针变量 INGETER num=10; printf("num=%d,p->num=%d\n",num,p->num); return 0; } 7.4 C++的引用
标签:

C语言基础(五)——结构体与C++引用由讯客互联互联网栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“C语言基础(五)——结构体与C++引用