1.指针与函数
1.1 函数名
include <stdio.h>
void Func(){
printf("Hello world!\n");
}
int main(){
Func();
printf("Func: %p\n",Func);
printf("&Func: %p\n",&Func);
}
1.2 函数指针
#include <stdio.h>
void Func(){
printf("Hello world!\n");
}
int Add(int a,int b){
return a+b;
}
int main(){
Func();
printf("Func: %p\n",Func);
printf("&Func: %p\n",&Func);
void (*p)() = Func;
// 函数指针
// p为函数指针
(*p)();
p();
// [] 运算符 ==> Func() p()
// int arr[];
// int* parr = arr;
// [] ==> parr[i]
int (*pAdd)(int a,int b) = Add;
printf("%d\n",(*pAdd)(1,2));
printf("%d\n",pAdd(1,1));
}
1.2.1 函数指针传入函数
#include <stdio.h>
int Add(int a,int b){
return a+b;
}
// 函数指针传入数组
int cal(int (*pAdd)(int,int),int a,int b){
return pAdd(a,b);
}
int main(){
int (*pAdd)(int a,int b) = Add;
printf("%d\n",cal(pAdd,1,3));
}
1.2.2 函数指针组成指针数组
#include <stdio.h>
int Add(int a,int b){
return a+b;
}
int Substract(int a,int b){
return a-b;
}
int Multipy(int a,int b){
return a*b;
}
int Divide(int a,int b){
return a/b;
}
int main(){
// 函数指针组成数组
int (*pcal[])(int a,int b) = {
Add,Substract,Multipy,Divide
};
printf("Add:%d\n",pcal[0](6,3));
printf("Substract:%d\n",pcal[1](6,3));
printf("Multipy:%d\n",pcal[2](6,3));
printf("Divide:%d\n",pcal[3](6,3));
}
- 与
main()
的参数结合
atoi()
:字符串转数字
头文件:#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>
// ./cal num1 +|-|*|/ num2
// 四个参数
int main(int argc,char** argv){
if(4 != argc){
printf("Error:参数不正确!\n");
printf("Usage:./cal num1 +|-|*|/ num2\n");
}
int num1 = atoi(argv[1]);
int num2 = atoi(argv[3]);
char operator = argv[2][0];
int res = 0;
switch(operator){
case '+':res = num1+num2;break;
case '-':res = num1-num2;break;
case '*':res = num1*num2;break;
case '/':res = num1/num2;break;
}
printf("%d %c %d = %d\n",num1,operator,num2,res);
}
-
switch-case
不支持字符串常量,只支持整型常量
- 字符串不能用
==
直接比较,用strcmp()
比较
include <stdio.h>
#include <stdlib.h>
#include <string.h>
int Add(int a,int b){
return a+b;
}
int Substract(int a,int b){
return a-b;
}
int Multipy(int a,int b){
return a*b;
}
int Divide(int a,int b){
return a/b;
}
int main(int argc,char** argv){
if(4 != argc){
printf("Error:参数个数不正确\n");
printf("Usage:./cal num1 +|-|*|/ num2\n");
}
int a = atoi(argv[1]);
int b = atoi(argv[3]);
char* operator = argv[2];
int res = 0;
// 字符串数组
char* operators[] = {"+","-","*","/"};
// 函数指针数组
int (*pcal[])(int a,int b) = {Add,Substract,Multipy,Divide};
for(int i=0;i<sizeof(operators)/sizeof(operators[0]);i++){
if(strcmp(operator,operators[i]) == 0){
res = pcal[i](a,b);
}
}
printf("%d %s %d = %d\n",a,operator,b,res);
}
- 数组排序
qsort()
-
cmp
:作为参数的函数成为回调(callback)函数
#include <stdio.h>
#include <stdlib.h>
int cmp(const void* a,const void* b){
printf("a:%p\t b:%p\n",a,b);
// 升序
return *(int*)a > *(int*)b ?1:-1;
//
// int* m = (int*)a;
// int* n = (int*)b;
// return *m > *n ?1:-1;
//
// 1: a放在b后面
//-1: a放在b前面
}
// ./arr_sort num1 num2 .. 排序
int main(int argc,char** argv){
int arr[argc-1];
for(int i=1;i<argc;i++){
arr[i-1] = atoi(argv[i]);
}
// 打印输入的数组
for(int i=0;i<argc-1;i++){
printf("%d ",arr[i]);
}
printf("\n");
// 排序
// qsort(数组地址,元素个数,元素大小,比较函数);
qsort(arr,argc-1,sizeof(int),cmp);
// 打印排序后的数组
for(int i=0;i<argc-1;i++){
printf("%d ",arr[i]);
}
printf("\n");
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int cmp(const void* p,const void* q){
printf("p:%p\t q:%p\n",*(char**)p,*(char**)q);
printf("p:%s\t q:%s\n",*(char**)p,*(char**)q);
// 正数:p放在q后面
// 负数:p放在q前面
// return *(char**)p - *(char**)q;
return strcmp(*(char**)p,*(char**)q);
}
int main(int argc,char** argv){
// 打印输入的字符串
for(int i=i;i<argc;i++){
printf("%s %p\n",argv[i],argv[i]);
}
printf("\n");
qsort(argv+1,argc-1,sizeof(char*),cmp);
for(int i=i;i<argc;i++){
printf("%s \n",argv[i]);
}
printf("\n");
}
2.3 指针函数