当前位置:网站首页>常量指针、指针常量
常量指针、指针常量
2022-07-24 10:18:00 【乘风破浪的阿呆】
C语言中的常量指针、指针常量在lua中的应用
常量指针是指针指向的内容是常量,可以有一下两种定义方式。
const int* n;
int const* n;
需要注意的是一下两点:
1、常量指针说的是不能通过这个指针改变变量的值,但是还是可以通过其他的引用来改变变量的值的。
int a=5;
const int* n=&a;
a=6;
2、常量指针指向的值不能改变,但是这并不是意味着指针本身不能改变,常量指针可以指向其他的地址。
int a=5;
int b=6;
const int* n=&a;
n=&b;
指针常量是指指针本身是个常量,不能在指向其他的地址,写法如下:
int*const n;
需要注意的是,指针常量指向的地址不能改变,但是地址中保存的数值是可以改变的,可以通过其他指向改地址的指针来修改。
int a=5;
int*p=&a;
int* const n=&a;
*p=8;
区分常量指针和指针常量的关键就在于星号的位置,我们以星号为分界线,如果const在星号的左边,则为常量指针,如果const在星号的右边则为指针常量。如果我们将星号读作‘指针’,将const读作‘常量’的话,内容正好符合。int const * n;是常量指针,int *const n;是指针常量。
指向常量的常指针
const int* const a;
是以上两种的结合,指针指向的位置不能改变并且也不能通过这个指针改变变量的值,但是依然可以通过其他的普通指针改变变量的值。
const int* const luaX_tokens [];
在lua源码中就是通过这种方式实现了一个常量数组,定义了一个常量指针指向这个常量数组。
边栏推荐
- 【机器人学习】机构运动学分析与matlab仿真(三维模型+word报告+matlab程序)
- Home raiding III (leetcode-337)
- Knapsack problem of dynamic programming -- three lectures on knapsack (01 knapsack, complete knapsack, multiple knapsack)
- What did zoneawareloadbalancer of ribbon and its parent class do?
- Source insight 3.5 comment garbled
- Jenkins deploys the project and prompts that the module package defined by him cannot be found
- Arduino- how to light the LED?
- Query about operating system security patch information
- [STM32 learning] (12) STM32 realizes LCD1602 simple static reality
- [STM32 learning] (22) STM32 realizes 360 degree rotary encoder
猜你喜欢

分布式锁-Redission 原理分析

The best time to buy and sell stocks (leetcode-121)

Homologous policy solutions

NIO知识点
![[STM32 learning] (15) STM32 realizes DHT11 temperature and humidity acquisition and display](/img/f2/6fcd4b2e747b4ceb52a52eda0c1af4.png)
[STM32 learning] (15) STM32 realizes DHT11 temperature and humidity acquisition and display

Mysql database JDBC programming

Curse of knowledge

MySQL 数据库 JDBC编程

757. Set the intersection size to at least 2: greedy application question

Add SSH key to bitbucket
随机推荐
高精尖中心论文入选国际顶会ACL 2022,进一步拓展长安链隐私计算能力
图模型2--2022-5-13
CRC Coding in C language
Mysql database JDBC programming
The optimal time to buy and sell stocks includes the freezing period (leetcode-309)
Uniapp calendar component
Activity exception lifecycle
[sword finger offer II 115. reconstruction sequence]
(3) Current situation of low code platform and R & D changes based on it basic components
Home raiding III (leetcode-337)
Installation UMI tutorial (error reporting and solutions)
Tag the specified commit and submit the tag
Kotlin advanced
What did zoneawareloadbalancer of ribbon and its parent class do?
[STM32 learning] (17) STM32 realizes LCD12864 display serial implementation
Basic SQL operations
Tree array-
zoj-Swordfish-2022-5-6
Dynamic planning: robbing families and houses
How does SRE and development of Google cooperate