一.指针函数
1.定义
指针函数是一个函数,其返回值是一个指针
例如:
void *function()
int *function()
float *function()
2.指针函数的返回值
指针函数的返回值要求是 全局变量的地址/static静态变量的地址/堆的地址(malloc申请的空间)/字符串常量的地址;
①.错误示范 - 返回局部变量的地址
若指针函数返回的是一个局部变量的地址,由于在函数运行完成后,其内部的局部变量的生命周期会结束,局部变量的资源会被回收,会导致返回的指针内部的内容不可控。
char *mystring()
{
char str[20]; //局部变量
strcpy(str,"hello");
return str; //返回局部变量的地址
}
int main(void)
{
printf("%s\n",mystring());
return 0;
}
其结果会导致段错误。
②.正确示范 - 返回静态变量的地址
指针函数要求返回 全局变量的地址/static静态变量的地址/堆的地址(malloc申请的空间)/字符串常量的地址 , 这里使用静态变量进行演示
char *mystring()
{
static char str[20]; //静态变量
strcpy(str,"hello");
return str; //返回静态变量的地址
}
int main(void)
{
printf("%s\n",mystring());
return 0;
}
3.练习
(1)删除字符串中的空格并返回
#include <stdio.h>
#include <string.h>
char *delete_space(char *str)
{
static char temp[64];
int j = 0;
/* 遍历该字符串,遇到空格就跳过,不是空格就存储起来 */
while(*str)
{
if(*str == ' ')
str++;
else
{
temp[j] = *str;
j++;
str++;
}
}
return temp;
}
int main(void)
{
printf("%s\n",delet_space("1 2 3 4 5 6 7 8 9"));
return 0;
}
(2).将整数转化为字符串,返回该字符串
#include <stdio.h>
#include <string.h>
char *itoa(int n)
{
static char p[64];
int i = 0,j = 0;
int temp_int = 0;
char temp_char;
/* 1.将整数的每一位提取出来,将那一位转成字符,存入数组中
例如传入1234,则提取4 3 2 1 存入数组中
*/
while(n != 0)
{
temp_int = n%10;
n = n/10;
p[i] = temp_int + '0'; //数字转字符
i++;
}
p[i] = '\0'; //最后加上一个结束符
i--; //移动到空字符的前一个位置
/* 2.将数组中的元素进行逆序,通过指针的位置进行数组元素相互交换 */
while(j < i)
{
temp_char = p[i];
p[i] = p[j];
p[j] = temp_char;
i--;
j++;
}
return p;
}
int main(void)
{
int n;
char *s;
printf("input >\n");
scanf("%d",&n);
s = itoa(n);
printf("%s\n",s);
return 0;
}
二.递归函数
1.定义
递归函数是指一个函数的函数体中直接或间接调用了该函数本身。
2.特点
递归函数调用的执行过程分为两个阶段:
①.递推阶段:从原问题出发,按递推公式递推,从未知到已知,最终达到递归终止条件。
②.回归阶段:按递归终止条件求出结果,逆向逐步带入递归公式,回归到原问题求解。
3.练习
①.递归函数求 n 的阶乘
#include <stdio.h>
#include <string.h>
int fac(int n)
{
if(n ==0 || n ==1) //递归结束条件
return 1;
return n * fac(n -1); //递归公式
}
int main(void)
{
int n,m;
printf("input >\n");
scanf("%d",&n);
m = fac(n);
printf("%d\n",m);
return 0;
}
②.递归函数实现斐波那契数列
#include <stdio.h>
#include <string.h>
int fib(int n)
{
if(n == 1 || n == 2) //递归结束条件
return 1;
return (fib(n-1) + fib(n-2)); //递归公式
}
int main(void)
{
int n,i;
printf("input >\n");
scanf("%d",&n);
for(i = 1;i <= n;i++)
{
printf("%d ",fib(i));
}
printf("\n");
return 0;
}
三.函数指针
1.定义
函数指针 是 指针,这个指针存放了函数的入口地址。
2.特点
①.函数名代表了函数的入口地址;
②.函数指针变量的一般形式为
<数据类型> (*<函数指针名称>)(参数说明列表)
#include <stdio.h>
#include <string.h>
int add(int a,int b);
int main(void)
{
int (*p)(int,int); //<数据类型> (*<函数指针名称>)(参数说明列表)
p = add; //使指针指向目标函数
printf("result : %d\n",add(10,20));
printf("result : %d\n",(*p)(10,20)); //(*p)解引用就代表函数名add
return 0;
}
/* 目标函数 */
int add(int a,int b)
{
return a+b;
}
四.函数指针数组
1.定义
首先,指针数组是一个数组,其内部存放的是指针。
函数指针数组,也是一个数组,其内部存放的是函数的指针
2.举例
函数指针数组内,存放多个函数的地址
#include <stdio.h>
#include <string.h>
int add(int a,int b);
int sub(int a,int b);
int main(void)
{
int (*p[2])(int,int); //<数据类型> (*<函数指针名称>)(参数说明列表)
p[0] = add; //第一个指针指向add函数
p[1] = sub; //第二个指针指向sub函数
printf("add result : %d\n",(*p[0])(10,20));
printf("sub result : %d\n",(*p[1])(10,20));
return 0;
}
/* 目标函数 */
int add(int a,int b)
{
return a+b;
}
int sub(int a,int b)
{
return a - b;
}