当前位置:网站首页>The use of pointers from the exchange of two data values (C language implementation)
The use of pointers from the exchange of two data values (C language implementation)
2022-08-10 06:14:00 【Write whatever you want.】
Article table of contents
I. Wrong practice of exchanging two data values
Error one:
void swap(int a, int b){int temp = a;a = b;b = temp;}int main(){int x=1;int y=2;swap(x,y);}
Error reason:mainWhen the function calls the swap function, the swap function allocates two spaces a and b to store the parameters of the main function. After the swap function is executed, the two values of a and b in the stack space allocated in the swap are exchanged.But when the swap function returns to the main function after execution, the two variables a and b are released, and the x and y in the main function have not changed.
Error two:
void swap(int* a, int* b){int* temp = a;a = b;b = temp;}int main(){int x=1;int y=2;swap(&x,&y);}
Error reason:GoWhen the function calls the swap function, the swap function allocates two pointer variables a and b to store the addresses of x and y. After the swap function runs, the addresses of the two pointer variables are exchanged, a points to y, and b points to x.After returning to the main function, the two pointer variables a and b are released, and the x and y of the main function remain unchanged.
Second, the exchange of two variables is worth the correct practice
Swap constant values:
void swap(int* a, int* b){int temp = *a;*a = *b;*b = temp;}int main(){int x=1;int y=2;swap(&x,&y);}
When the main function calls the swap function, the swap function allocates two pointer variablesa and b point to x and y respectively. The swap function directly accesses the value pointed to by the pointer through *p and exchanges them. When the swap function returns to the main function, the two pointer variables a and b are released, but the x,y is changed.
Swap pointer variable values:
void swap(int** c, int** d){int* temp = *c;*c = *d;*d = temp;}int main(){int x = 1;int y = 2;int* a = &x;int* b = &y;swap(&a, &b);}
When the main function calls the swap function, two pointers are allocated in the swap functionThe variables c and d respectively point to the pointer variables a and b in the main function. When the swap function is executed, the two pointer variables a and b are manipulated through the two pointer variables c and d to achieve the purpose of exchanging the two pointer variables.Only the addresses stored in the pointer variables a and b will be changed, and the values of the constants x and y will not be changed.
void swap(int* c, int* d){int temp = *c;*c = *d;*d = temp;}int main(){int x = 1;int y = 2;int* a = &x;int* b = &y;swap(a, b);}
When the main function calls the swap function, two pointers are allocatedThe variables c and d are used to store the values of the pointer variables a and b, that is, c and d point directly to the variable values, and the variable values are exchanged through *P, so in the end, the variables x and y will change, while the addresses stored in a and b are notwill change.
Summary
The sub-function needs to operate on the variables of the main function. The incoming parameter in the sub-function is the address of the main function variable. If the main function is an ordinary variable, the incoming parameter should be a pointer. If the main function is a pointer variable, the incoming parameter should be a second-level pointer, which is pushed in in turn.Then the sub-function uses the pointer to change the value of the variable of the main function. Do not directly operate the pointer in the sub-function, but operate the pointed variable through *p.The last case is similar to exchanging constant values, in that it changes the value of the constant without changing the pointer to the constant in the main function.
边栏推荐
猜你喜欢
随机推荐
在Unity中让主摄像机发射一条射线,判断射线在游戏场景中所碰撞的游戏物体名字和标签名称(亲测有效)
每日刷题(day02)——leetcode 622. 设计循环队列
markdown使用技巧
hanLP探索-语义距离计算的实现
优化Mysql运行OrderBy性能
如何实现网格建造系统
氨氮吸附材料原理
电路建模的要点
STC12C5A60S2单片机WIFI信号扫描报警监视系统信号增强信号过低报警
氨氮吸附工艺
unity守则(随时持续更新\自我总结)
STM32单片机手机APP蓝牙高亮RGB彩灯控制板任意颜色亮度调光
解析树字符串并输出中序遍历
剑指 Offer(第 2 版)7/12 18-20
溶液中重金属去除
解决错误 Could not find method leftShift() for arguments
浅谈游戏中3种常用阴影渲染技术(2):阴影锥
51单片机智能蓝牙APP加油站火灾预警安防防控报警监控系统MQ2DHT11
开源游戏服务器框架NoahGameFrame(NF)简介(一)
酸回收工艺讲解