指向指针的指针是一种多级间接寻址的形式,或者说是一个指针链。通常,一个指针包含一个变量的地址。当我们定义一个指向指针的指针时,第一个指针包含了第二个指针的地址,第二个指针指向包含实际值的位置。
一个指向指针的指针变量必须如下声明,即在变量名前放置两个星号。例如,下面声明了一个指向 int 类型指针的指针:
int **var
#include <iostream.h>
int main ()
{
int var;
int *ptr;
int **pptr;
var = 100;
// 获取 var 的地址
ptr = &var;
// 使用运算符 & 获取 ptr 的地址
pptr = &ptr;
// 使用 pptr 获取值
cout << "var 值为 :" << var << endl;
cout << "*ptr 值为:" << *ptr << endl;
cout << "**pptr 值为:" << **pptr << endl;
cout << "&var 为 :"<< &var <<endl ;
cout << "ptr 为 :"<< ptr <<endl ;
cout << "&ptr 为 :"<< &ptr <<endl ;
cout << "pptr 为 :"<< pptr <<endl ;
cout << "&ptr 为 :"<< &pptr <<endl ;
return 0;
}
/* 运行结果为
var 值为 :3000
*ptr 值为:3000
**pptr 值为:3000
var 地址为 :0x0019FF3C
ptr 地址为 :0x0019FF3C
ptr 地址为 :0x0019FF38
*/
C++ 传递指针给函数,只需要简单地声明函数参数为指针类型即可。在函数内部,*ptr是得到传入指针,原本指向地址的值。
#include <iostream>
using namespace std;
void Fun_Num(unsigned int *par);
int main ()
{
unsigned int num;
cout<<"输入一个数字";
cin>>num ;
Fun_Num(&num);
// 输出实际值
cout << "The num is :" << num << endl;
return 0;
}
void Fun_Num(unsigned int *par)
{
*par = *par + 1;
}
/*输出结果为
输入一个数字99
The num is :100
*/
以前我们说过,数组名字代表的是指向该数组的常量指针,所以我们同样可以传入数组给函数。
#include <iostream>
using namespace std ;
double Get_Balance(int *ptr , int size );
void main()
{
int data[5] = {1,2,3,4,5};
double balance = 0 ;
balance = Get_Balance(data,5);
cout << "Ths balance value is :" << balance <<endl ;
}
double Get_Balance(int *ptr , int size )
{
int sum = 0 ;
int i = 0 ;
double balance = 0 ;
for(i = 0 ; i < size ; i ++)
{
sum = sum + ptr[i];
}
balance = sum / size ;
return balance ;
}
/* 输出结果为
Ths balance value is :3
*/
同样的,我们可以传入一个指针到某一个函数,那么我们也可以在某一个函数中返回一个指针。为了做到这点,必须声明一个返回指针的函数,如下:
int * Function()
{
.
.
.
}
另外,C++ 不支持在函数外返回局部变量的地址,除非定义局部变量为 static 变量。
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std ;
float Get_Balance(int *ptr , int size );
int *Get_num(void);
void main()
{
float balance = 0 ;
balance = Get_Balance(Get_num(),5);
cout << "Ths balance value is :" << balance <<endl ;
}
int *Get_num()
{
static int data[5] ;
srand( (unsigned)time( NULL ));
for (int i = 0; i < 5; i++)
{
data[i] = rand()%10;
cout << data[i] << endl;
}
return data;
}
float Get_Balance(int *ptr , int size )
{
int sum = 0 ;
int i = 0 ;
float balance = 0 ;
for(i = 0 ; i < size ; i ++)
{
sum = sum + ptr[i];
}
balance = sum / size ;
cout << balance << endl ;
return balance ;
}
/* 输出结果为
9
2
1
7
2
4
Ths balance value is :4
*/
函数指针 与 指针函数 分析(重点与难点)
指针函数是指带指针的函数,即本质是一个函数,函数返回类型是某一类型的指针
指针函数在上面已经提及过了,什么是指针函数。就是返回值是一个指针的函数。声明方法如下:
类型说明符 * 函数名(参数)
int * A(int B,int C);
函数指针是指向函数的指针变量,即本质是一个指针变量。
先看一段代码
其中 void (*fun)(); 是函数指针的声明,并没传入任何函数进去。
void Output_1(void); 声明了一个函数。
fun = Output_1; 将一个函数的地址(函数名就代表了该函数的地址)赋值给了函数指针。这样 fun 这个函数指针就具备了Output_1这个函数的功能。
这就是函数指针的用法。
#include <iostream>
using namespace std ;
void (*fun)();
void Output_1(void);
void main()
{
fun = Output_1;
(*fun)();
}
void Output_1(void)
{
cout<<"1"<<endl;
}
/* 输出结果为
1
*/
还有一个更高级的声明方式
typedef void (*FunP)();
可以看出这是一个无返回类型的函数指针,不带任务参数。后面我们就可以像定义普通变量一样,轻松的定义函数指针了。
函数指针的用处
将函数作为参数传递给函数(此部分转自https://blog.csdn.net/yuexiang321/article/details/52658947)
相信,大家中学的时候都学过积分这个鬼东西。不知道还记不记得积分最原始的计算方法。对,没错,无限细分,求面积。好的。我们接下来就给大家一个应用函数指针有关积分计算的例子。
直接贴出代码。
#include <iostream>
using namespace std;
//Calculate用于计算积分。一共三个参数。第一个为函数指针func,指向待积分函数。二三参数为积分上下限
double Calculate(double(*func)(double x), double a, double b)
{
double dx = 0.0001;//细分的区间长度
double sum = 0;
for (double xi = a+dx; xi <= b; xi+=dx)
{
double area = func(xi)*dx;
sum +=area;
}
return sum;
}
double func_1(double x)
{
return x*x;
}
double func_2(double x)
{
return x*x*x;
}
void main()
{
cout<<Calculate(func_1, 0, 1)<<endl ;
cout<<Calculate(func_2, 0, 1)<<endl ;
// printf("%lf\n", Calculate(func_1, 0, 1));
// printf("%lf\n", Calculate(func_2, 0, 1));
}
/* 输出结果为
0.333383
0.25005
*/
通过函数指针,我们可以在函数中使用别的函数作为参数。此程序可以完成对不同函数的积分。
2)引用不在代码段中的函数
此功能在嵌入式系统中经常使用。我们知道,我们写的用户程序的code是存放在代码段中的,在嵌入式系统中,一般情况下是存放在flash中的。什么叫不在代码段中的函数?很多微控制器在出厂前会将一些功能函数(系统函数)固化在rom中(类似于PC机中的BIOS),如Flash擦写功能,Flash Copy功能。而我们写的代码是不认识这些函数的,不能直接使用函数名调用。所以,当我们想在用户程序中调用这些系统函数时,就只能使用函数指针的方式,通过将系统函数的入口地址传给函数指针,来达到调用rom中程序的目的。这些系统函数一般都会在官方手册中给出功能,返回值类型和参数列表。
下面是从三星的S5PV210_applicationnote中截取的一个系统函数。
从上我们可以分析出,此系统函数的入口地址为0xD0037F98。返回bool型,带有int, unsigned int, unsigned short, unsigned int*, bool型五个参数。实际使用时,我们可以如下调用:
// 实际使用时
pCopySDMMC2Mem p1 = (pCopySDMMC2Mem)0xD0037F98;
p1(x, x, x, x, x);
传入五个合适的参数即可。
部分资料来自于CSDN博客,菜鸟教程!!!!