一、C语言之函数指针
#include<stdio.h> #include<stdlib.h> /** 函数指针 */ //定义一个函数 int add(int x,int y){ return x+y; } main(){ //定义函数指针 int (*android)(int x, int y); //函数指针赋值 android = add; //使用函数指针 int result = android(12,15); printf("result==%d\n",result); system("pause"); }
输出到控制台内容如下:
二、C语言中联合体的理解
#include<stdio.h> #include<stdlib.h> /** Unition联合体 */ //定义一个联合体,特点,所有的字段都是使用同一块内存空间 union Mix{ long i;//4个字节 int k; //4个字节 char ii; //1个字节 }; main(){ printf("mix:%d\n",sizeof(union Mix)); //实验 union Mix m; m.i = 100; m.k = 123; m.ii = 50; printf("m.i==%d\n",m.i); printf("m.k==%d\n",m.k); printf("m.k==%d\n",m.ii); system("pause"); }
输出到控制台内容如下:
三、C语言中的枚举
#include<stdio.h> #include<stdlib.h> /** 枚举的值是递增的 默认首元素的值是0 */ enum WeekDay{ Monday=0,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday }; main(){ enum WeekDay day = Wednesday; printf("%d\n",day); system("pause"); }
输出到控制台内容如下:
四、C语言中的结构体
#include<stdio.h> #include<stdlib.h> /** 结构体 */ //定义结构体 struct student{ int age; float score; char sex; }; main(){ //使用结构体 struct student stu = { 18,95.6,'W' }; //结构体取值 printf("stu.age==%d\n",stu.age); printf("stu.score==%.1f\n",stu.score); printf("stu.sex==%c\n",stu.sex); //结构体赋值 stu.age = 20; stu.score = 100; stu.sex = 'M'; printf("stu.age==%d\n",stu.age); printf("stu.score==%.1f\n",stu.score); printf("stu.sex==%c\n",stu.sex); //结构体的长度 printf("struct student 结构体的长度==%d\n",sizeof(struct student)); system("pause"); }
输出到控制台内容如下:
五、C语言中结构体指针
#include<stdio.h> #include<stdlib.h> /** 结构体指针 */ //定义结构体 struct student{ int age; float score; char sex; }; main(){ //使用结构体 struct student stu = { 18,95.6,'W' }; //结构体指针 struct student* point = &stu; struct student** point2 = &point; //取值运算 (*point).age 等价于 point->age printf("(*point).age ==%d\n",(*point).age); printf("point->age ==%d\n",point->age ); printf("point->score ==%f\n",point->score ); printf("point->sex ==%c\n",point->sex ); //赋值运算 point->age = 20; point->score = 100; point->sex = 'M'; printf("point->age ==%d\n",point->age); printf("point->score ==%f\n",point->score); printf("point->sex ==%c\n",point->sex ); //二级结构体指针取值 (*point).age 等价于 point->age 所以 (**point).age 等价于 (*point)->age printf("(**point2).age ==%d\n",(**point2).age); printf("(*point2)->age ==%d\n",(*point2)->age ); printf("(*point2)->score ==%f\n",(*point2)->score ); printf("(*point2)->sex ==%c\n",(*point2)->sex ); //二级结构体指针赋值运算 (*point2)->age = 60; printf("(*point2)->age ==%d\n",(*point2)->age); system("pause"); }
输出到控制台内容如下: