当前位置:网站首页>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.
边栏推荐
猜你喜欢
随机推荐
mkfs.minix.c之minix_super_block.s_ninodes获取解析
如何实现网格建造系统
动态规划、背包问题 6/22 96-100
unity3d著名项目-Dark Tree翻译
每日刷题(day03)——leetcode 899. 有序队列
21天学习挑战赛--分班
每日刷题(day02)——leetcode 622. 设计循环队列
STM32F407ZG GPIO输入相关实验
电路分析中的电容器的基本知识
hanLP探索-语义距离计算的实现
二叉树 6/15 76-80
序列化、编码、requests库json和data参数
Tkinter 模块学习
常用模块封装-pymysql、pymongo(可优化)
qemu和主机共享磁盘
Share a professional TA's "Shader Reference"
自定义View的流程总结学习
优化Mysql运行OrderBy性能
ASP.NET连接SQL Server的步骤
二叉树 6/16 81-85









