函数名做函数参数
记下来备忘,基础牢固一些,会减少一些不必要的错误
1 //定义比较函数max(int,int) 2 int max(int a,int b) 3 { 4 return a>b?a:b; 5 } 6 //指针fun作为compare函数的形式参数1,表明compare函数的形参1为 7 // 有2个 int型 参数的 函数指针(或称之为函数地址/入口) 8 //注意:int (*fun)(int,int)并不需要fun指针指向函数的参数 如下调用时*** 9 //compare内部调用fun指针指向的函数,此函数的参数为c,d 10 int compare(int (*fun)(int,int),int c,int d) 11 { 12 return fun(c,d); 13 } 14 15 int main(int argc, char *argv[]) 16 { 17 int temp1 = 0,temp2 = 3; 18 //*** 19 //调用时,实参只需填入参数为(int,int)型的函数地址(函数名即位函数地址) 20 printf("max = %2d\r\n",compare(max,temp1,temp2)); 21 }
运行结果:
宏定义#undef以及多行宏定义
#undef 将已定义的宏定义撤销;
多行宏定义 宏定义内容过长或为了阅读便利而将一行内容拆分为多行
#ifdef MYDEFINE #undef MYDEFINE #endif #define STR1 "This is my\ //符号"\"连接本行与下一行,作用相当于字符串的重载"+" my test\ string"
主函数内容如下:
#ifdef MYDEFINE printf("use mydefine\r\n"); #else printf("%s\r\n",STR1); #endif
运行结果如下:
小结
越是基础,细节的东西,越要重视。