当前位置:网站首页>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 .
边栏推荐
- Leetcode 1184: distance between bus stops
- Queue assistant | product update log in June 2022
- [Star Project] small hat aircraft War (IV)
- 基于C51实现led流水灯
- The method of raspberry pie connecting tmall elf speaker to play music through Bluetooth
- Precious metal knowledge: lethal short-term secret script
- Difference between shape and size ()
- MySQL self incrementing lock
- docker修改挂载到宿主机上的mysql配置文件不生效?
- Idea -- use @slf4j to print logs
猜你喜欢

Contents mismatch at: 08000000H (Flash=FFH Required=00H) ! Too many errors to display !

"Wei Lai Cup" 2022 Niuke summer multi school training camp 1 supplementary question record (acdgij)
![Rgb-t tracking - [dataset benchmark] gtot / rgbt210 / rgbt234 / vot-2019-2020 / laser / VTUAV](/img/10/40d02da10a6f6779635dc820c074c6.png)
Rgb-t tracking - [dataset benchmark] gtot / rgbt210 / rgbt234 / vot-2019-2020 / laser / VTUAV

Idea -- use @slf4j to print logs

mySql建表的基本操作 与常见的函数

20220725 自动控制原理中的补偿器

Opengauss simple version installation error

Is the passenger flow always low? There is something wrong with the location of your store!

Drools(4):Drools基础语法(2)

buuReserve(4)
随机推荐
NiO implementation
Realize the full link grayscale based on Apache APIs IX through MSE
NPM command
Shared lock
[untitled] reprint
Kubernetes scheduling concept and workflow
[romance understood by technical talents] tidb community has prepared a gift for your partner for the "Tanabata Festival". Reply: if I want to challenge, I can participate in the activity!
Rgb-t tracking - [dataset benchmark] gtot / rgbt210 / rgbt234 / vot-2019-2020 / laser / VTUAV
How regular expressions write variables
Opencv learning basic functions
MySQL table read lock
替换license是否要重启数据库?
MySQL intent lock
What are the ways to open the JDBC log of Youxuan database
Acwing- daily question
[yiku] wonderful! This library organization NPM script is simply explosive!
Airiot IOT platform enables the container industry to build a welding station information monitoring system
【一库】妙啊!这个库组织npm脚本简直爆炸!
JIT中的IR工具与JITWatch的下载,编译及使用
From scratch, we will completely develop an online chess game [Gobang] Based on websocket, and only use dozens of lines of code to complete all the logic.