“返回指针的函数”与“指向函数的指针”
『返回指针的函数』和『指向函数的指针』非常相似,使用时特别注意区分。
返回指针的函数定义:char * upper(char *str)
指向函数的指针:char (*fun)(int int)
返回指针的函数:
char * upper(char *str) { char *dest = str; while(*str != NULL) { if(*str >= 'a' && *str <= 'z') { *str -= 'a' - 'A'; } str++; } return dest; }
指向函数的指针:
int add(int a,int b) { return a+b; } int min() { int (*fun)() = add; int result = (*fun)(1,2); char hello[] = "Hello"; char *dest = upper(heLLo); }
int result = (*fun)(1,2); 是指向 add 函数的函数,执行结果 result=3。
char *dest = upper(heLLo); 将 hello 字符串中的小写字母全部转换为大写字母。
参考自:https://www.runoob.com/cplusplus/cpp-return-arrays-from-function.html