当前位置:网站首页>Differences in the use of function call pointer parameters *p, * & P
Differences in the use of function call pointer parameters *p, * & P
2022-07-26 07:11:00 【The way of growth of Peng】
The following procedure , The comparison shows that when the function call pointer , Parameter is *p、*&p The difference between
#include<iostream>
#include<stdio.h>
using namespace std;
void change2(int *x){
cout<<"change2: x->"<<x<<endl;//x Namely p Shape parameter ,x The value is a The address of
cout<<"change2: &x->"<<&x<<endl;//&x Namely x The address of
cout<<"change2:*&x->"<<*&x<<endl;//*&x Namely x The value pointed to by the address of , That is to say a The address of
++x;
}
void change1(int *&x){
cout<<"change1: x->"<<x<<endl;//x Namely p Actual parameters ,x The value is a The address of . It can be understood as *(&x), What's coming in is p In itself
cout<<"change1: &x->"<<&x<<endl;//&x Namely x The address of
cout<<"change1:*&x->"<<*&x<<endl;//*&x Namely x The value pointed to by the address of , That is to say a The address of
++x;
}
int main(){
int a = 10,b = 20;
int *p1 = &a,*p2 = &b;
// cout<<*p<<endl;
cout<<"&a:"<<&a<<",p1:"<<p1<<endl;
change1(p1);
cout<<"p1 after change1:"<<p1<<endl;
cout<<endl<<endl;
// p = a;
cout<<"&b:"<<&b<<",p2:"<<p2<<endl;
change2(p2);
cout<<"p2 after change2:"<<p2<<endl;
// cout<<*p<<endl;
return 0;
}

We can see , It's also a pointer , The form of the passed in parameter is *p、*&p In the function of and after self addition , The result is different .
This explanation , If you don't write &, The pointer passed in is the formal parameter , Analogy to main Functional a、b Two variables are passed in void swap(int a,int b) The function is the same , about main Function a、b There will be no exchange ;
And wrote & Function of , It's like main Functional a、b Two variables are passed in void swap(int &a,int &b) In exchange for main function a、b Two variable values , Incoming in the form of address is a、b The actual address of , Will naturally main Functional a、b The values of the two variables are exchanged .
in short :x The value of is a The address of ,&x yes x The address of , and *&x It is x The value of the address of , That is to say a The address of ( The address of the address ).
If the address character is not passed &, Pointer parameter ; If the address character is passed &, Is a pointer argument .
边栏推荐
- With Huawei cloud welink, you can connect to the world even in the countryside
- NIO实现
- File server fastdfs
- Rectification ideas for the previous article
- 123123123
- Make a chase game with pyGame
- LTS(Light-Task-Scheduler)
- MySQL table write lock
- Docker modifying the MySQL configuration file attached to the host does not take effect?
- SQL shell (PSQL) tool under PostgreSQL
猜你喜欢

Realize the full link grayscale based on Apache APIs IX through MSE

Common CMD instructions

Drools (3): drools basic syntax (1)

Basic operations and common functions of MySQL table creation
![[database] CTE (common table expression)](/img/36/812026995f5d0b64d26f1667638010.png)
[database] CTE (common table expression)

DaemonSet

Screen: frame paste, 0 fit, full fit

Yolov6 target detection practice: training your own data set (video tutorial)

Opencv learning color detection

The results of the soft test can be checked, and the entry to query the results of the soft test has been opened in the first half of 2022
随机推荐
[Star Project] small hat aircraft War (III)
Drools (3): drools basic syntax (1)
Yolov5 improvements: add attention mechanism (video tutorial)
[yiku] wonderful! This library organization NPM script is simply explosive!
正则表达式如何写变量
Kernel pwn 入门 (5)
< II> ObjectARX development: create and edit basic graphic objects
Introduce you to JVM from architecture
怎样在win10家庭版中使用Hyper-V
The method of raspberry pie connecting tmall elf speaker to play music through Bluetooth
20220725 自动控制原理中的卷积Convolution
Manifest merger failed with multiple errors, see logs
Advanced Mathematics (Seventh Edition) Tongji University exercises 2-5 personal solutions
Flame diagram analysis Flink backpressure
RGB-T追踪——【数据集基准】GTOT / RGBT210 / RGBT234 / VOT-2019-2020 / LasHeR / VTUAV
[hard ten treasures] - 7.2 [dynamic RAM] analysis of the difference between DDR4 and DDR3
What are the basics of getting started with spot silver
MySQL read / write lock
Basic operations and common functions of MySQL table creation
屏:框贴、0贴合、全贴合