指向指针(函数)的指针

当一个指针指向一个指针时,这个指针保存后者指针的地址。指针函数也相同。如,指针a指向指针b,那么指针a的值即是指针b的地址。


#include <iostream>

using namespace std;

#define LOGE(e) cout << #e << "=" << e << endl;

void pointer(int* const p) {
	(*p)++;
	//*p++; //error
	cout << "pointer:" << *p << endl;
}

void reference(int& r) {
	r++;
	cout << "reference:" << r << endl;
}

void ponit_reference()
{
	int i = 5;

	pointer(&i);
	cout << "i:" << i << endl;

	reference(i);
	cout << "i2:" << i << endl;

/*
pointer:6
i:6
reference:7
i2:7
*/
}

void mutil_pointer()
{
	int i = 5;
	int *p = &i;
	int **p2 = &p;
	int ***p3 = &p2;
	
	LOGE(p);
	LOGE(p2);
	LOGE(p3);

	LOGE(*p);
	LOGE(*p2);
	LOGE(*p3);
	
	LOGE(*p);
	LOGE(**p2);
	LOGE(***p3);

/* output

p=0015FE20
p2=0015FE14
p3=0015FE08
*p=5
*p2=0015FE20
*p3=0015FE14
*p=5
**p2=5
***p3=5

*/
}

int multi(int i)
{
	return i*i;
}

void func_pointer()
{
	//int (*pfun) (int) = multi;
	int (*pfun) (int);
	pfun = multi;
	LOGE(pfun);

	int (**pfun2) (int);
	pfun2 = &pfun;
	LOGE(pfun2);
	LOGE(*pfun2);

	int r1 = (*pfun)(2);
	LOGE(r1);

	int r2 = (**pfun2)(3);
	LOGE(r2);

/* output

pfun=013C15F5
pfun2=0027F694
*pfun2=013C15F5
r1=4
r2=9

*/
}

void main()
{
	ponit_reference();

	mutil_pointer();

	func_pointer();

getchar();
}


你可能感兴趣的:(指向指针(函数)的指针)