当前位置:网站首页>指针得真正奥义!!!
指针得真正奥义!!!
2022-07-26 22:49:00 【helinliupi-何】
目录
理解指针:
指针所指向的时变量的地址,并且可以通过指针直接改变变量的值
例如:
#include<stdio.h>
int main()
{
int a = 0; //定义int型变量
int* p = &a; //定义int型指针并指向a的地址(&表示地址)
*p = 1; //p指针指向的地址的值赋值为1,相当于a=1
printf("%d\n", a);
return 0;
}结果a = 1
![]()
首先:指针定义的类型必须与所指变量类型相同。如上图都为int型。
其次:定义指针可以理解为先int* p; 再p = &a; (p指向a的地址)这个也是正确书写,我上面写的就相当于把2个p融合为1个p来书写。
最后:*p是等于a的值,就是*p == a;所以*p改变,a的值也要改变。
真正奥义:
普通函数换值
在没指针情况下在函数中改变传递变量值
#include<stdio.h> void hanshu(int b) //函数解释 a传值给b { b = 1; //在函数中使b赋值为1 } int main() { int a = 0; hanshu(a); //函数调用 printf("%d\n", a); return 0; }结果:a = 0

用指针在函数里换值
依然是传变量,在函数里再用指针
#include<stdio.h>
void hanshu(int b) //函数传值给b
{
int* p = &b; //指针p指向b的地址
*p = 1; //指针p指向b的地址的值赋值为1
}
int main()
{
int a = 0;
hanshu(a); //函数调用
printf("%d\n", a);
return 0;
}结果:a = 0;说明在函数中指针定义并不能改变main函数里的传值变量
![]()
* 3.用函数传地址给指针
#include<stdio.h>
void hanshu(int *p) //函数解释使a的地址传递给p(相当于指针p指向a的地址)
{
*p = 1; //指针p指向a的地址的值赋值为1
}
int main()
{
int a = 0;
hanshu(&a); //函数调用
printf("%d\n", a);
return 0;
}结果:a = 1
![]()
总结:
1.可以看出,普通函数里换传递的值改变不了main函数里的变量的值
2.用函数传递地址,用指针接收可以在函数里改变main函数里的变量的值,这也是最重要的指针用法。
谢谢大家阅读!!!
边栏推荐
- TreeSet集合存储元素的问题
- lvs+keepalived项目实战
- CF 1333C Eugene and an array
- First acquaintance with C language (1)
- VLAN原理简述、具体实验配置
- (title + detailed idea + annotated code) codeforces round 805 (Div. 3) F Equate Multisets
- (史上最详细)Codeforces Round #805 (Div. 3)E. Split Into Two Sets
- Educational Codeforces Round 132 (Rated for Div. 2), problem: (D) Rorororobot
- RS-485总线通信应用
- Ogeek meetup phase I, together with cubefs, is hot
猜你喜欢

OSPF的重发布及路由策略

NAT(网络地址转化协议)

数字集成电路:MOS管器件章(二)
![[Database Course Design] SQLSERVER database course design (student dormitory management), course design report + source code + database diagram](/img/24/fe01d656a54086adf8a3702b82cc26.png)
[Database Course Design] SQLSERVER database course design (student dormitory management), course design report + source code + database diagram

数字芯片的面积优化:第三届“华为杯”研究生创芯大赛数字方向上机题1详解

2022 latest live broadcast monitoring 24-hour monitoring (III) analysis of barrage in live broadcast room

广域网技术实验

动态路由ofps协议配置

Nat网络地址转换实验

(前缀和/思维)Codeforces Round #806 (Div. 4)F. Yet Another Problem About Pairs Satisfying an Inequality
随机推荐
数字集成电路:MOS管器件章(二)
HCIA基础知识(1)
Nb-iot access to cloud platform
第五讲—按键控制LED
HCIA动态路由RIP基础实验
CF 1333C Eugene and an array
OSPF协议知识汇总
HCIA基础知识(1)
Mechanical hard disk Selection Guide -- from the selection experience
三个整数从大到小排序(详细介绍多种方法)
Golang — 解析 yaml 文件
2022 latest live broadcast monitoring 24-hour monitoring (III) analysis of barrage in live broadcast room
6.30 didi surface warp (one side + two sides)
Codeforces Round #810 (Div. 2), problem: (B) Party
(超详尽版,不懂随时评论)Codeforces Round #804 (Div. 2)C The Third Problem
HCIA Basics (1)
RS-485 bus communication application
OSPF configuration in mGRE environment and LSA optimization - reduce the amount of LSA updates (summary, special areas)
Static comprehensive experiment (comprehensive exercise of static route, loopback interface, default route, empty interface, floating static)
最新C语言入门与进阶 -史上最全最详细的C语言教程!! 第一节-总览C语言概括