#define _CRT_SECURE_NO_WARNINGS //写一个地址传递的函数,实现两个变量的内容互换
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <Windows.h>
int sun(int* a, int* b);//声明子函数体,子函数体需要写在主函数外,我的子函数体写函数在最后面,在主函数体前面声明函数,可以提前告知这个函数有什么用 主函数里可以提前引用
int main(void)
{
int n = 10;
int m = 20;
sun(&n, &m);//调用函数,输入变量n=10的地址
printf("n:%d,m:%d\n", n, m);
return EXIT_SUCCESS;
}
int sun(int* a, int* b) {//定义指针类型的int,解引用*a找到*a的地址&n,&n值为10,换算过来就是 int* a=&n, n=10;
int c = 0;//在子函数体里面定义变量c
c = *a;//三杯水案例转换变量值,*a *b通过解引用找到指针地址
*a = *b;
*b = c;
return 0;
}
地址传递的函数,实现两个变量的内容互换
最新推荐文章于 2024-09-08 20:29:34 发布