第一节:结构体对齐-结构体数组
1. 结构体的定义和初始化,结构体数组
C语言提供结构体来管理不同类型的数据组合。例如:一名学生有学号、姓名、性别、年龄、地址等属性。如果针对学生的这些属性单独定义一个变量,那么在有多名学生时,变量就难以分清。这是就需要用到结构体。
声明结构体类型的一般形式:
struct 结构体名{成员列表};
先声明结构体名,再定义变量名。
struct student s1;
struct student s2;
#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,"beijing"};printf("%d %s %c %d %f %s\n",s.num,s.name,s.sex,s.age,s.score,s.addr); // 定义及初始化printf("-------------------------\n");struct student sarr[3];int i;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;
}
F:\Computer\Project\practice\8\8.1-statement\cmake-build-debug\8_1_statement.exe
1001 lele M 20 85.400002 beijing
-------------------------
1002 lala M 25 86.4 tianjin
1003 lilei W 23 87.2 shanxi
1004 hubing M 22 88.3 qingdao
1002 lala M 25 86.400002 tianjin
1003 lilei W 23 87.199997 shanxi
1004 hubing M 22 88.300003 qingdao进程已结束,退出代码为 0
对比java中的对象来理解此部分。
结构体类型声明要放在main函数之前,这样main函数中才可以使用这个结构体,工作中往往把结构体声明放在头文件中。
如果结构体变量已经定义,那么只能对它的每个成员单独赋值,如s.num=1005。
2. 结构体对齐
结构体的大小必须是其最大成员的整数倍。
#include <stdio.h>struct student_type1{double score; // double是一种浮点型,占8个字节short age; // short是短整型,占2个字节
};
struct student_type2{double score;int height;short age;
};
struct student_type3{int height;char sex;short age;
};int main() {struct student_type1 s1;struct student_type2 s2;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));retu rn 0;
}
F:\Computer\Project\practice\8\8.1-alignment\cmake-build-debug\8_1_alignment.exe
s1 size=16
s2 size=16
s3 size=8进程已结束,退出代码为 0
分析:对于student_type1,因为最大成员double占8个字节,所以最少需要16个字节。student_type2同理。student_type3最大成员int占了4个字节,所以最少需要8个字节。
思考:为什么要对齐?
为了CPU更高效取内存数据。有时,我们可以通过变换属性的位置来调节占用空间的大小,比如将char和short放到一起,他们会共同占用4个字节。
第二节:结构体指针与typedef的使用
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',1002,"shangsan",'W',1003,"lili",'M'};struct student *p; // 定义结构体指针int num;p=&s;printf("%d %s %c\n",p->num,p->name,p->sex);p=sarr;printf("%d %s %c\n",(*p).num,(*p).name,(*p).sex); // 方式一,获取成员printf("%d %s %c\n",p->num,p->name,p->sex); // 方式二,获取成员printf("-------------------------\n");p=p+1;printf("%d %s %c\n",p->num,p->name,p->sex);p=p+1; // 注意指针已经在上面的基础上偏移了,所以还是加1printf("%d %s %c\n",p->num,p->name,p->sex);return 0;
}
F:\Computer\Project\practice\8\8.2-morphology-pointer\cmake-build-debug\8_2_morphology_pointer.exe
1001 wangle M
1001 lilei M
1001 lilei M
-------------------------
1002 shangsan W
1003 lili M进程已结束,退出代码为 0
解析:p就是一个结构体指针,可以对结构体s取地址并赋给p,p就可以访问结构体每个成员。而数组名存储的是数据首地址,所以将sarr赋给p。
“.”成员选择器的优先级高于“*”运算符,所以需要加括号,通过*p得到sarr[0],然后获取对应成员。
2. typedef的使用
可以使用typedef声明新的类型名来代替已有的类型名。相当于取别名。
#include <stdio.h>// 结构体指针
typedef struct student{int num;char name[20];char sex;
}stu,*pstu; // 对定义的student结构体取别名typedef int INTEGER; // 对已有的类型取别名int main() {stu s={1001,"wangle",'M'};pstu p;p=&s;printf("p->num=%d,p->name=%s,p->sex=%c\n",p->num,p->name,p->sex);INTEGER i=10;printf("i=%d\n",i);return 0;
}
F:\Computer\Project\practice\8\8.2-typedef\cmake-build-debug\8_2_typedef.exe
p->num=1001,p->name=wangle,p->sex=M
i=10进程已结束,退出代码为 0
解析:使用stu定义结构体变量和使用struct student定义结构体变量是等价的。使用INTEGER定义变量i和使用int定义变量i是等价的。pstu等价于struct student*,所以p是结构体指针变量。
第三节:C++引用和C++布尔类型
1. C++的引用
对于C++,首先新建源文件时,名字需要叫main.cpp,以cpp后缀结尾。
C++引用能够使编码更加方便快捷。
#include <stdio.h>
// C语言代码
void modify_num(int *b){*b=*b+1;
}int main() {int a=10;modify_num(&a);printf("after modify_num a=%d\n",a);return 0;
}
#include <stdio.h>
// C++代码
void modift_num(int &b){b=b+1;
}int main() {int a=10;modift_num(a);printf("after modift_num a=%d\n",a);return 0;
}
以上两种编码,结果均为:
F:\Computer\Project\practice\8\8.3-c++_code\cmake-build-debug\8_3_c___code.exe
after modift_num a=11进程已结束,退出代码为 0
子函数内修改主函数的一级指针变量。
#include <stdio.h>
// C语言代码
void modify_pointer(int **p,int *q){*p=q;
}
int main() {int *p=NULL;int i=10;int *q=&i;modify_pointer(&p,q); // p是一级指针,再取地址就是二级指针printf("after modify_pointer *p=%d\n",*p);return 0;
}
#include <stdio.h>
// C++代码
void modify_pointer(int *&p,int *q){p=q;
}int main() {int *p=NULL;int i=10;int *q=&i;modify_pointer(p,q);printf("after modify_pointer *p=%d\n",*p);return 0;
}
以上两种编码,结果均为:
F:\Computer\Project\practice\8\8.3-class_a_c\cmake-build-debug\8_3_class_a_c.exe
after modify_pointer *p=10进程已结束,退出代码为 0
说明:p本身就是指针变量,在传递时就是二级指针。
2. C++的布尔类型
布尔类型在C语言中没有,是C++的,有true和false。
#include <stdio.h>int main() {bool a=true;bool b=false;printf("a=%d,b=%d\n",a,b);return 0;
}
F:\Computer\Project\practice\8\8.3-bppl\cmake-build-debug\8_3_bppl.exe
a=1,b=0进程已结束,退出代码为 0
说明:在c++中,true为1,false为0。