当前位置:网站首页>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 .
边栏推荐
- UVa11991 Easy Problem from Rujia Liu
- SAP WM preliminary transaction code lx29 - list of fixed storage bins
- leetcode:210. 課程錶 II
- 没有学历,自学软件测试,找到一份月入过万的测试工作真的有可能吗?
- Transaction code qs28 of SAP QM preliminary level
- Listener in JSP
- 初步了解认识正则表达式(Regex)
- Teamwork collaboration application experience sharing | community essay solicitation
- 居家办公期间如何提升沟通效率|社区征文
- Research Report on market supply and demand and strategy of China's hydraulic injection molding machine industry
猜你喜欢

It has been engaged in the functional test of 10K to the test development of 40W annual salary for 5 years, and spent 7 days sorting out the super comprehensive learning route

#113 Path Sum II

torch. nn. Linear() function

The required books for software testers (with e-books) recommended by senior Ali have benefited me a lot

#141 Linked List Cycle

Image processing 12- image linear blending

Solution of multi machine room dynamic loop status network touch screen monitoring

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

Double carbon in every direction: green demand and competition focus in the calculation from the east to the West

Library cache lock brought by add trandata
随机推荐
China hydraulic cylinder linear position sensor market trend report, technical dynamic innovation and market forecast
Teamwork collaboration application experience sharing | community essay solicitation
#886 Possible Bipartition
test
nn. PReLU(planes)
在同花顺开户安全么,买股票怎么网上开户
#141 Linked List Cycle
torch. nn. Linear() function
Minio client (MC command) implements data migration
To understand Devops, you must read these ten books!
Data visualization - biaxial comparison effect
Do we media video, and share the necessary app for friendly new media operation
Listener in JSP
二分查找
CUDA out of memory
At the beginning of SAP QM, assign sampling strategy for quantitative characteristics
What are the barriers that Huawei terminals need to cross if they want to rely on the intelligent turnover of the whole house?
竣达技术丨适用于“科士达”智能精密空调网络监控
atoi超强解析
leetcode:210. Schedule II