Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference between const int* and int* const in C language?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article is to share with you about the difference between const int* and int* const in C language. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

# include using namespace std;/* learns the space of const embellishment. Const int a; int const a; const int* a; / / the modified a points to the memory space, int* const a; / / modifies the a variable. Const int* const a; / / the pointer variable and the memory space the pointer points to cannot be modified. How to understand this, modify the pointer, then what is the difference between the pointer and the variable? to put it bluntly, it is an address. What is the concept of protecting a pointer? if it is the protected value pointer variable itself, then it is no different from int* const. So, think a little further away, will it protect the value of the variable pointed to by the pointer can not be modified? Looking back, you will find that const has done a road blocking operation, int* const: you can not directly manipulate variables, such as modifying the contents of variables, to put it bluntly, it is the value in memory. Const* int: you can't manipulate the contents of memory indirectly through pointers, and infer: in the entire C language system, there is another operation that can directly modify the value in memory. Const int* summary: determines what kind of way to limit the modified memory, who is close to const, determines the way to limit, const is close to int*, indicating that this is a refusal to modify memory space indirectly. Const is close to the variable name, indicating that this is a refusing to modify the memory space directly. * / void test1 () {int a = 10; int c = 100; const int* b; / / decorates the future space and can be assigned on another line. / / this operation modifies the b variable, not the effect const int* wants, b = & c; / * b = 100; / / this is the correct verification method, int* const d = & a; / / if the modification is a variable, then the value must be given during initialization, not another line of assignment. / / d = & c; / / this modifies the d variable. If you change the value of the d variable, the program will prompt an error. / * Summary: to put it bluntly, it is to protect the value of the variable itself or the value of the space referred to by the variable. The scope of protecting data is different. How to use a value that protects current resources and a value that is more like protecting more distant resources? The location of the variable: the definition of the variable and the assignment operation of the variable, according to the above description, will encounter the above two situations. The performance of variables on function arguments 1 if the parameters of the function are like const int*, what does it mean? inside the function, it can be modified to refer to the value in the memory space where the dependent variable is located. 2 if the parameter of the function is int* const a, then inside the function, the variable a cannot be assigned, the representation of the variable on the return value of the function, 1 if the return value of the function is like const int*, is that OK? 2 if the return value of the function is like int* const Is that okay。 The return value depends entirely on what state the recipient is and how to deal with it, directly or indirectly modify the memory. The modification of * /} / * is that the space of the pointer corresponding to the variable cannot be modified. * / void test1 (const int* a) {/ / * a = 100; / / the performance is no different from the normal assignment and is not allowed to be modified. A = (int*) 0x01; return;} void test2 (int* const a) {int c = 100; / a = & c; / / a variable cannot be modified, return The return value of} / * const int* is meaningless. The recipient must be a function of type const int*. If you do the operation of * var= * * to the recipient, like the ordinary operation of variable assignment, it still depends on the constraint ability of const int*, * / const int* test3 () {static int b = 5 Static int* a = & b; return a;} / * is the same as test3. After the function returns, the recipient's status indicates an and, * / int* const test4 () {static int c = 3; static int* a = & c; return a;} int main () {int a = 10; test1 (& a); const int* p = test3 () / / * p = 100; / / this is not allowed. It embellishes the future space, cout

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report