当前位置:网站首页>Pointer and array & pointer and const & struct and Const
Pointer and array & pointer and const & struct and Const
2022-06-12 21:05:00 【Bald and weak】
Pointers and arrays :
Pointer can access array subscript
for example :
void Show(int* p, int length)// Output function
{
for (int i = 0; i < length; i++)
printf("%d", p[i]);// perhaps printf("%d", *(p+i));p[i] Equivalent to *(p+i)
}
int main()
{
int arr[] = { 1,2,3,4 };
Show(arr, sizeof(arr) / sizeof(arr[0]));
}
The pointer and const:
const Used to decorate functions and variables
const Modify the constant :
const int a=10;//a Is a constant variable , Do not modify the
const int * And int* const:
const int *p=&a; // allow p Point to a, But it's not modifiable a,p Can point to anything .a Is a constant , No modification allowed .
int* const p=&a; // here ,const modification p Variable ,p Can only point to a, You can also modify a( At present only if a It's a variable. ), however p It is not allowed to point to .
( Memory can be understood in this way , Only remember which cannot change :const int *p=&a,const And then int *p, because *p=a, therefore a The value of does not change ;int* const p=&a,const And then p, modification p,p The contents of the grid are a The address of ,p By const, So the grid doesn't change ,p The direction of .
Refer to this article to understand : I use #CSDN# This app Found a technical blog , My friends ask for the same 《int const* And int * const》, Let's have a look around https://blog.csdn.net/qq_33248019/article/details/89462405?utm_source=app&app_version=5.0.0
const The wrong use of :
(1)const int a=10;
int* p=&a;// because p No modification allowed a, So the compiler warns
(2)const int *p=&a;// limit P Don't modify a
*p=&b;
(3)const int a=10;
int *const p=&a;// Will report a mistake , This means p You can modify a Value , however a It's a constant variable again
const char* name and char name[10] The difference between :
1. In memory : Pointer occupied 4 Bytes , The memory of the array may be large
2. Want to change the character , It must be an array .
Structure and const:
There is a point of understanding to emphasize :
eg:
struct Student
{
const char* name;
int age;
char sex;
};
int main()
{
struct Student s1 = { " Li Si ",14," male "};
}
explain : Define in the structure const char* name, You can't change the name , Correct usage .
But if const Embellish gender , Can be s1.sex=‘ Woman ’ Change direction .“ male ” It is amended as follows “ Woman ”, But you can't “ male ” It is amended as follows “ men ”, It means Not to change the original character , Instead, it changes the direction of the variable .
边栏推荐
- Mxnet record IO details
- remote: Support for password authentication was removed on August 13, 2021
- JS深浅拷贝
- 跳槽前恶补面试题,金三成功上岸腾讯,拿到30k的测开offer
- 做自媒体视频,友好的新媒体运营必备app分享
- #886 Possible Bipartition
- Large and small end conversion
- USB mechanical keyboard changed to Bluetooth Keyboard
- leetcode:207. 课程表
- Junda technology is applicable to "kestar" intelligent precision air conditioning network monitoring
猜你喜欢

Shell language

leetcode:207. 课程表

Is it really possible to find a testing job with a monthly income of more than 10000 without a degree and self-study software testing?

Summary of machine learning materials

Introduction to scala basic grammar (III) various operators in Scala

Before job hopping, Jin San made up the interview questions. Jin San successfully landed at Tencent and got a 30K test offer

leetcode:210. Schedule II

Successful transition from self-study test halfway, 10K for the first test

New product release Junda intelligent integrated environmental monitoring terminal

Solve the cvxpy error the solver GLPK_ MI is not installed
随机推荐
China hydraulic cylinder linear position sensor market trend report, technical dynamic innovation and market forecast
#141 Linked List Cycle
Mxnet record IO details
Introduction to scala basic grammar (III) various operators in Scala
Understanding of functions
杨辉三角代码实现
Can flush open an account? Can you directly open the security of securities companies on the app? How to open an account online when buying stocks
Cv2.lut() (populates the output array with values from the lookup table)
Data visualization - broken line area chart
SAP QM preliminary - cannot assign a sampling policy to an inspection characteristic when maintaining an inspection plan by executing transaction code qp02?
JS deep and shallow copy
同花顺能开户吗,同花顺在APP上可以直接开通券商安全吗 ,买股票怎么网上开户
#141 Linked List Cycle
USB mechanical keyboard changed to Bluetooth Keyboard
Research Report on market supply and demand and strategy of China's hydraulic injection molding machine industry
Leetcode: 210. Programme II
Algorinote_2_主定理与 Akra-Bazzi 定理
Access control system based on RFID
Double carbon in every direction: green demand and competition focus in the calculation from the east to the West
Pytorch how to set random number seed to make the result repeatable