当前位置:网站首页>The pointer is really profound!!!
The pointer is really profound!!!
2022-07-27 02:24:00 【Helinliupi he】
Catalog
Ordinary function exchange value
Use the pointer to exchange values in the function
* 3. Pass address to pointer with function
Understand the pointer :
The address of the time variable pointed to by the pointer , And you can directly change the value of the variable through the pointer
for example :
#include<stdio.h>
int main()
{
int a = 0; // Definition int Type variable
int* p = &a; // Definition int Type pointer and point to a The address of (& Address )
*p = 1; //p The value of the address pointed to by the pointer is assigned 1, amount to a=1
printf("%d\n", a);
return 0;
}result a = 1
![]()
First : The type of pointer definition must be the same as the type of the indicated variable . As shown in the above figure int type .
secondly : Defining a pointer can be understood as first int* p; Again p = &a; (p Point to a The address of ) This is also written correctly , What I wrote above is equivalent to 2 individual p Merge into 1 individual p To write .
Last :*p Is equal to a Value , Namely *p == a; therefore *p change ,a The value of should also be changed .
True mystery :
Ordinary function exchange value
Change the value of the passed variable in the function without a pointer
#include<stdio.h> void hanshu(int b) // Function interpretation a The value to b { b = 1; // Make... In a function b The assignment is 1 } int main() { int a = 0; hanshu(a); // Function call printf("%d\n", a); return 0; }result :a = 0

Use the pointer to exchange values in the function
It is still a variable , Use pointers in functions
#include<stdio.h>
void hanshu(int b) // Function passes value to b
{
int* p = &b; // The pointer p Point to b The address of
*p = 1; // The pointer p Point to b The value of the address of is assigned 1
}
int main()
{
int a = 0;
hanshu(a); // Function call
printf("%d\n", a);
return 0;
}result :a = 0; Note that the pointer definition in the function cannot be changed main Value passing variables in functions
![]()
* 3. Pass address to pointer with function
#include<stdio.h>
void hanshu(int *p) // Function interpretation makes a The address of is passed to p( Equivalent to pointer p Point to a The address of )
{
*p = 1; // The pointer p Point to a The value of the address of is assigned 1
}
int main()
{
int a = 0;
hanshu(&a); // Function call
printf("%d\n", a);
return 0;
}result :a = 1
![]()
summary :
1. It can be seen that , The value passed in an ordinary function cannot be changed main The value of a variable in a function
2. Pass address with function , Receiving with a pointer can be changed in a function main The value of a variable in a function , This is also the most important pointer usage .
Thank you for reading !!!
边栏推荐
- Codeforces Round #810 (Div. 2), problem: (B) Party
- Use of golang - sync package (waitgroup, once, mutex, rwmutex, cond, pool, map)
- 光光光仔的CSDN之旅
- MySQL课程1.简单命令行--简单记录 欢迎补充纠错
- Golang bufio Reader 源码详解
- Dynamic routing ofps protocol configuration
- Wechat applet: user wechat login process (attached: flow chart + source code)
- Codeforces Round #809 (Div. 2), problem: (C) Qpwoeirut And The City
- 离开页面的提示
- HCIP-第一天
猜你喜欢

(题意+详细思路+加注释代码) Codeforces Round #805 (Div. 3)F. Equate Multisets

在有序数组找具体某个数字

Dynamic routing ofps protocol configuration

C语言——第一个程序、打印、变量和常量
![[C language] relevant distinction between strlen and sizeof](/img/c0/c026818692a01c1867771434e90da8.png)
[C language] relevant distinction between strlen and sizeof

OSPF basic configuration application (comprehensive experiment: interference election default routing area summary authentication -- interface authentication)

Experiment of OSPF in mGRE environment

RISC-V工具链编译笔记

MGRE, PPP, HDLC comprehensive experiment

记录第N次SQL异常
随机推荐
MySQL course 1. simple command line -- simple record welcome to supplement and correct errors
Nb-iot access to cloud platform
The latest C language introduction and advanced - the most complete and detailed C language tutorial in history!! Section 1 - overview of C language
HCIP-第六天-OSPF静态大实验
WAN technology experiment
Static comprehensive experiment (comprehensive exercise of static route, loopback interface, default route, empty interface, floating static)
NAT network address conversion experiment
Codeforces Round #810 (Div. 2), problem: (B) Party
指针得真正奥义!!!
(CF1691D) Max GEQ Sum
HCIA Basics (1)
Self introduction and planning about programming
Hcip day 1
NAT (network address translation protocol)
Codeforces Round #796 (Div. 2), problem: (1688C) Manipulating History
RS-485 bus communication application
静态路由综合实验
HandsomeForum学习论坛
Full company mGRE and star topology mGRE
C语言——while语句、dowhile语句、for循环和循环结构、break语句和continue语句