20191210 使用二级指针实现将函数内部的数据带出来

/*
以下是错误的用法,打印出来会报错,原因查明是引用了空指针,传递给函数以后,实际他本身没有任何变化
void  boy_home(int* meipo) {
	static int boy = 19;
	meipo = &boy;
}

int main() {
	int x = 10, y = 100;
	swap(&x, &y);
	printf_s("x=%d,y=%d\n", x, y);

	int* meipo = NULL;
	boy_home(meipo);
	printf_s("boy=%d\n", *meipo);


	system("pause");
	return 0;
}
*/
#include <Windows.h>
#include <iostream>

using namespace std;
int swap(int* a, int* b) {
	int temp = *a;
	*a = *b;
	*b = temp;
	return 0;
}

void  boy_home(int** meipo) {
	static int boy = 19;
	*meipo = &boy;	
}

int main() {
	int x = 10, y = 100;
	swap(&x, &y);
	printf_s("x=%d,y=%d\n", x, y);

	int* meipo = NULL;
	boy_home(&meipo);
	printf_s("boy=%d\n", *meipo);


	system("pause");
	return 0;
}

运行过程分析如图:在这里插入图片描述
运行结果如图:
在这里插入图片描述

发布了51 篇原创文章 · 获赞 0 · 访问量 552

猜你喜欢

转载自blog.csdn.net/weixin_40071289/article/details/103484326