当前位置:网站首页>9. Pointer of C language (3) classic program, exchange the value of two numbers for deep analysis, (easy to understand), are formal parameters and arguments a variable?
9. Pointer of C language (3) classic program, exchange the value of two numbers for deep analysis, (easy to understand), are formal parameters and arguments a variable?
2022-07-28 20:05:00 【A programmer who loves playing badminton】
This article is the... Of the pointer series 3 piece , If it feels good , You can follow and collect , It will be updated continuously in the future
Catalog
Let's verify whether the arguments and formal parameters are variables
Classic program , Exchange the values of two numbers through a function
Method 1: Exchange... Through intermediate variables
Method 2: Exchange by changing the pointer direction of formal parameters
Before understanding this content , We need to understand a very important knowledge point first , Formal parameters and actual parameters
problem 1: What are formal and actual parameters ?
Formal parameters are when we define functions , Parameters written in parentheses
The argument is when we call the function , Incoming parameter
The number of formal and actual parameters must be the same , The positions should correspond one by one , Data types should be compatible
problem 2: Are formal parameters and arguments a variable ?
No , Formal and actual parameters are never a variable
Let's verify whether the arguments and formal parameters are variables
#include <iostream>
#include <stdio.h>
using namespace std;
void test(int i)
{
i = 99;
printf(" Shape parameter i The address for :%#X\n", &i);
}
int main(int argc, char const *argv[])
{
int i = 6;
printf(" Actual parameters i The address for %#X\n", &i);
test(i);
printf(" after test Function to get i by %d\n", i);
return 0;
}
i By function test after ,i The value of has not changed , And the shape parameter i And real parameters i The address of is different , This further illustrates that arguments and formal parameters are not variables .
Classic program , Exchange the values of two numbers through a function
Method 1: Exchange... Through intermediate variables
void exchange(int a, int b)
{
int t;// Define intermediate variables
t = a;
a = b;
b = temp;
}
int main(int argc, char const *argv[])
{
int a = 3;
int b = 5;
exchange(a, b);
printf("----------------------\n");
printf("a=%d\n", a);
printf("b=%d\n", b);
return 0;
}This method doesn't work , Arguments will a=3,b=5 Pass into formal parameter a,b in , Only formal parameters are exchanged , After the function runs, the space is released , Actual parameters a and b The value of is still the original value , Because arguments and formal parameters are not variables

Method 2: Exchange by changing the pointer direction of formal parameters
void exchange_2(int *p, int *q)
{
int *t;// Define intermediate pointer variables t
t = p;
p = q;
q = t;
}
int main(int argc, char const *argv[])
{
int a = 3;
int b = 5;
exchange_2(&a, &b);// Pass in a and b The address of
printf("----------------------\n");
printf("a=%d\n", a);
printf("b=%d\n", b);
return 0;
}Method 2 Can not be , Because this way
Although it was a and b The address of ( The pointer ), namely p Save the a The address of ,q preservation b The address of , But what has changed is p and q The direction of
Before change :

After change :

Although what has changed is p,q The direction of , But our output is still a and b
Method 3: Change by address
void exchange_3(int *p, int *q)
{
int t;
// If you want to swap *p and *q Value , be t Must be defined as int, Cannot be defined as int*, Otherwise syntax error
t = *p; //p The data type of is int*,*p The data type of is int
*p = *q;
*q = t;
}
int main(int argc, char const *argv[])
{
int a = 3;
int b = 5;
exchange_3(&a, &b);
printf("----------------------\n");
printf("a=%d\n", a);
printf("b=%d\n", b);
return 0;
}Method 3 feasible
Into a,b The address of ,p and q There is a,b The address of ,*p It's a real parameter a,*q It's a real parameter b, In exchange for *p and *q It's equivalent to exchange a and b. let me put it another way , Is to find through the address a and b Then change their values .
Before change :

After change :

summary :
1. Formal and actual parameters will never be a variable
2. To exchange the values of two numbers through a function , Is to change the address . The address of the number to be changed is passed in as a parameter
3. When writing the p = &a; After this sentence ,p What's stored is a The address of ,*p Namely a
4. After the change ,a,b Your address will not change
边栏推荐
- English translation Portuguese - batch English conversion Portuguese - free translation and conversion of various languages
- Cell review: single cell methods in human microbiome research
- Read how to deploy highly available k3s with external database
- Concurrent programming, do you really understand?
- Return and job management of saltstack
- A chip company fell in round B
- Labelme (I)
- Android section 13 03xutils detailed explanation of database framework (addition, deletion and modification)
- Sprint for golden nine and silver ten, stay up at night for half a month, collect 1600 real interview questions from Android post of major manufacturers
- English translation Arabic - batch English translation Arabic tools free of charge
猜你喜欢
![[C language] simulation implementation of pow function (recursion)](/img/7b/ef8b3d97adc7810de249a37642c71f.png)
[C language] simulation implementation of pow function (recursion)

ssm中项目异常处理

软考中级(系统集成项目管理工程师)高频考点

Implementation of strstr in C language

JS batch add event listening onclick this event delegate target currenttarget onmouseenter OnMouseOver

The results of the second quarter online moving people selection of "China Internet · moving 2022" were announced

Edge detection and connection of image segmentation realized by MATLAB

Hebei: stabilizing grain and expanding beans to help grain and oil production improve quality and efficiency

数字滤波器设计——Matlab
![[C language] simulation implementation of strlen (recursive and non recursive)](/img/73/e92fe714515491f1ea366d6924c9ec.png)
[C language] simulation implementation of strlen (recursive and non recursive)
随机推荐
Use Hal Library of STM32 to drive 1.54 inch TFT screen (240*240 st7789v)
Use of strtok and strError
Const pointer of C language and parameter passing of main function
There is a 'single quotation mark' problem in the string when Oracle inserts data
[网络]跨区域网络的通信学习IPv4地址的分类和计算
Redis notes
Implementation of strstr in C language
2022年下半年系统集成项目管理工程师认证8月20日开班
Token verification program index.php when configuring wechat official account server
2、 Relationship between software operation and memory
Basic usage of docker
Getting started with enterprise distributed crawler framework
Leetcode day2 连续出现的数字
[C language] Pointer advanced knowledge points
[network] cross area network communication learning classification and calculation of IPv4 address
Saltstack advanced
Stories of Party members | Li qingai uses cartoons to drive farmers to increase income and become rich
[C language] shutdown game [loop and switch statement]
[C language] function
Leetcode day3 employees who exceed the manager's income