当前位置:网站首页>Exchange variable values with and without pointers in C language
Exchange variable values with and without pointers in C language
2022-07-23 05:32:00 【Vegetable chicken autotrophic process】
stay C In language , We often encounter the situation of exchanging two values , Generally, you will choose to exchange the addresses of two numbers through the pointer and really exchange two numbers . Some friends don't quite understand the difference between the two , Here is a detailed explanation .
Classic mistake :
#include<stdio.h>
void swap(int x, int y);
int main()
{
int a = 100;
int b = 200;
printf(" Before the exchange a=%d,b=%d", a, b);
swap(a, b);
printf("\n After the exchange a=%d,b=%d", a, b);
return 0;
}
void swap(int x, int y)
{
int temp = x;
x = y;
y = temp;
}
When I first came into contact with functions and pointers, I really didn't understand why the values of two numbers didn't exchange , Even if I will a,b Set to global variable , It is still impossible to exchange two values directly .
Later I learned that , The two values in the above code are exchanged , The function being called swap in ,a and b The values of are indeed exchanged , But when you jump out swap After the function , Everything is nothing ,a Is still a,b Is still b. amount to a and b Made a “swap” Dream , In the dream, two people exchange identities . After waking up ,a Is still a,b Is still b, All exchanges are floating clouds .
And why change the address through the pointer , You can change two values ? Because when the pointer points to an assigned variable , What is stored in the memory space of the address saved by the pointer is the binary form of the value assigned by the variable , That is to say, the pointer and the value are bound together , When you change the address , The variable is still the original variable , Values have been exchanged with the exchange of pointers .
The details are as follows :
Pointer control exchanges two values
First understand , The relationship between variable address and variable value .
Variable is the address of the first byte of the memory it occupies ( That is, the starting address ) To represent the address of the variable in memory . Each variable has a unique address , Is the location in memory , As shown in the figure below , Integer variables x Occupy from 0028FF1C~0028FF1F Four bytes of memory space , Variable x The address of is 0028FF1C.
secondly , The value of each variable is stored in the memory space represented by the variable address . For instance variable x Value 10 It is stored in binary form in the compiler as x Of the four spaces allocated . You can directly access the value of a variable through its name .

and Pointer to the variable Is the variable used to save the address . The definition of pointer is as follows :
int a=100;
int *z=&a;The meaning of the above two sentences :a It's an integer variable , The assignment is 100;z Is a pointer variable , It points to a The address of , The memory space represented by this address stores values 100 In binary form . After address exchange , The contents of the memory space represented in the address will also be exchanged .
When we assign values to two integer variables , And set two pointer variables to point to these two integer variables respectively :
int a=100,b=200;
int *z=&a;
int *y=&b;stay main Call in function a,b The address of :
swap(&a,&b); //&a and &b Is the actual parameter , The relay will be a,b The address of When in swap When exchanging in function , The parameter must be a,b The address of :
void swap(int* M, int* N) //M,N It's a formal parameter ,M It stands for a The address of ,N representative b The address of
{
int temp=*M; //*M Said by M Indirect access gets M Value , namely a Value
*M=*N;
*N=temp;
} The complete code is as follows :
#include<stdio.h>
void swap(int* M, int* N);
int main()
{
int a = 100;
int b = 200;
int* z = &a;
int* y = &b;
printf(" Before the exchange a=%d,b=%d", a, b);
swap(&a, &b);
printf("\n After the exchange a=%d,b=%d", a, b);
return 0;
}
void swap(int* M, int* N)
{
int temp = *M;
*M = *N;
*N= temp;
}The debugging results are as follows :
I hope the above can be helpful to the understanding of some partners !
边栏推荐
- 二叉树—堆
- Low in SoC_ Power simple control
- Code random notes_ Array_ 704 binary search
- Leetcode-504. Hex number
- C language implementation of three chess
- Detailed explanation of C language linked list & Implementation of two important linked lists
- My redis collation
- Detailed explanation of Axi agreement
- [Err] 1064 - You have an error in your SQL syntax;
- 仅用5000行代码,在全志V853上AI渲染出一亿幅山水画
猜你喜欢

APB protocol details vs. 3.0-4.0-5.0

IDEA使用指南

大一暑假实习day7总结

leetcode-343. 整数拆分

Introduction to C language arrays, functions, operators, and keywords

获取电脑硬件信息

大一暑假实习day5_1

Basic operation of linked list

Super detailed - how to understand the expression while (scanf ("%d", & Num)! = EOF) in C language?

Learned on October 17
随机推荐
注解和反射笔记
Filter拦截器和过滤器
Binary SCA fingerprint extraction black Technology: go language Reverse Technology
Operation on December 1
leetcode-204.计数质数
Exercise + floating point stored in memory
CreateProcess 输出重定向
JDBC-API详解
Leetcode - the best time to buy and sell stocks, including handling charges
交换二进制位中的奇偶位
Several simple and understandable methods of finding prime numbers in C language
代码随想录笔记_链表_19两两交换链表中的节点
练习+浮点型在内存中存储
Detailed explanation of advanced data types in C language
My redis collation
Code random notes_ Array_ 209 subarray with the smallest length
Today's homework blog
代码随想录笔记_链表_142环形链表II
Day7 summary of freshman Summer Internship
了凡四训的业障重表现
