函数指针、指针函数与结构体的基础知识与应用
一、指针函数1.概念本质是指针返回值是指针2.定义格式数据类型 *函数名(参数列表){函数体return 地址}3.应用实例#includestdio.h #includestdlib.h #includestring.h char *fun() { // char p[10] hello; //开辟在栈区函数运行完结束不一定存的是hello // char *p hello; //存在常量区hello不能修改 char *p (char *)malloc(sizeof(char)*10); //开辟在堆区未释放可读可修改 strcpy(p,hello); return p; } int main(int argc, char const *argv[]) { char *p fun(); printf(%s\n,p); strcpy(p,word); printf(%s\n,p); free(p); p NULL; return 0; }注意区分值传递和地址传递需要看针对于那个变量。如果传参传变量本身对于该变量来说就是值传递如果传参传变量的地址对于变量来说就是地址传递。二、函数指针1.概念本质是指针指向的是函数。2.格式数据类型 (*指针名) (参数列表)数据类型需要和指向函数的数据类型保持一致参数列表需要和指向函数的参数列表保持一支3.基本用法//////////1.定义函数指针并赋值/////////////////// #include stdio.h int sum(int a, int b) { return a b; } int sub(int a, int b) { return a - b; } int main(int argc, char const *argv[]) { int (*p[2])(int, int) {sum, sub}; printf(%d\n, sum(1,2)); printf(%d\n, p[0](5,2)); printf(%d\n, p[1](5,2)); return 0; } /////////////2.函数传参////////////////#includestdio.h int add(int a,int b) { return ab; } int sub(int a,int b) { return a-b; } void test(int (*p)(int,int)) { printf(%d\n,p(2,3)); } int main(int argc, char const *argv[]) { test(add);//5 test(sub);//-1 return 0; }4.函数指针数组1.概念本质数组存放的是指针2.格式数据类型 (*数组名[元素个数])(形参列表);数据类型需要和指针指向的函数类型保持一致参数列表需要和指针指向的参数列表保持一致3.赋值int (*p[2])(int, int) {函数名1, 函数名2};#includestdio.h int add(int a,int b) { return ab; } int sub(int a,int b) { return a-b; } int main(int argc, char const *argv[]) { int (*arr[2])(int,int){add,sub}; //arr[0] add;arr[1]sub; printf(%d\n,arr[0](2,3));//5 return 0; }三、结构体1.概念结构体是用户自定义的数据类型在结构体中可以包含若干个不同数据类型和不同意义的数据项当然也可以相同从而使这些数据相组合起来反应某一个对象的信息。2.格式struct 结构体类型名{数据类型 成员变量;数据类型 成员变量;数据类型 成员变量;.......};struct student { int id; float score; char name[32]; };3.结构体变量3.1 概念结构体类型的变量3.2 格式struct 结构体类型名 变量名;struct student stu;3.3 定义结构体变量1先定义结构体类型再单独定义结构体变量例如#includestdio.h struct student { int id; float score; char name[32]; }; int main(int argc, char const *argv[]) { struct student stu1; return 0; }2定义结构体类型的同时定义结构体变量例如struct student { int id; float score; char name[32]; }stu;也可以缺省结构体类型名定义结构体变量但不常用struct { int id; float score; char name[32]; }stu;3.4结构体变量赋值1定义结构体变量的同时通过{}整体赋值struct student stu1 {1,77,zhangsan};2定义结构体变量的同时用点等法赋值struct student stu2 { .id 2, .score 90.1, };3先定义结构体变量后对成员变量单独赋值truct student stu3; stu3.id 3; stu3.score 97.9; strcpy(stu3.name,lisi);3.5 访问成员变量格式结构体变量名.成员变量名#include stdio.h #include string.h struct student { int id; float score; char name[32]; }stu; int main(int argc, char const *argv[]) { struct student stu1; scanf(%d %f %s,stu1.id,stu1.score,stu1.name); printf(%d %.2f %s\n,stu1.id,stu1.score,stu1.name); return 0; }四、结构体数组1.概念结构体类型的数组2.定义格式struct 结构体类型名 数组名[元素个数];1先定义结构体类型再单独定义结构体数组#includestdio.h struct student { int id; int class; char name[32]; }; int main(int argc, char const *argv[]) { struct student stu1[3]; return 0; }2定义结构体类型的时候同时定义结构体数组struct student { int id; int class; char name[32]; }stu[3];3.结构体数组赋值1定义结构体数组的同时赋值struct student stu1[3]{ {1,98.5,zhangsan}, {2,67.8,lisi}, {3,78.5,wangwu} };3先定义结构体数组在对数组的每一个元素分别赋值struct student st[3]; //给数组的第一个元素赋值 st[0].id 1; st[0].score 90.6; strcpy(st[0].name,haha);4.结构体的输入与输出下标做为循环变量遍历输入输出#include stdio.h #include string.h struct student { int id; float score; char name[32]; } ; int main(int argc, char const *argv[]) { struct student stu[3]; for (int i 0; i 3; i) scanf(%d %f %s,stu[i].id,stu[i].score,stu[i].name); for (int i 0; i 3; i) printf(%d %.2f %s\n,stu[i].id,stu[i].score,stu[i].name); return 0; }五、结构体指针结构体类型的指针1.定义、赋值struct 结构体类型名 *指针名#includestdio.h #includestdlib.h struct student { int id; float score; char name[32]; }; int main(int argc, char const *argv[]) { struct student stu; struct student *p stu; return 0; }2.指针访问成员变量指针名-成员变量名struct student stu; struct student *p stu; p-id 1; p-score 98.5;3.大小本质是指针32位操作系统4字节64位操作系统8字节