How to use pointers and const modifiers
This article mainly explains the "pointer and const modifier how to use", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "pointer and const modifier how to use" it!
# # pointers to constants
If you want a pointer to point to a constant, you can declare it by adding const before a pointer variable, that is, the object being pointed to is a constant, so p is a constant and the value of p cannot be changed.
Const int * p; constant pointer
The pointer to the right of the const qualifier on the right of the * sign is itself a const pointer, because the pointer itself is a constant, so the compiler requires that it be given an initialization value, and the pointer must be initialized at the same time as the declaration. That is, the pointer p is constant and cannot point to another address after initialization.
# include int main () {int x = 45; int const sum = 100; int * const p = & x; int * const p2 = ∑ printf ("% d\ n% d\ n", * pjingp2); int y = 55; x = y; printf ("% d\ n", * p); * p = sum; printf ("% d\ n", * p); int * p1 = p Printf ("% d", * p1); return 0;}
# # constant pointer to constant
A pointer to a constant can be declared and then initialized, so you can point the pointer to a constant.
# # pointers to constants to ordinary variables
Although p cannot be negative, the value of the variable can be modified directly to achieve the effect of modifying p.
# include int main () {int x = 256; const int y = 88; const int * p; int * p1; p = & y; printf ("% d\ n", * p); p = & x; printf ("% d\ n", * p); x = 128; printf ("% d\ n", * p); P1 = (int *) & y; printf ("% d\ n", * p1); return 0 } Thank you for your reading, the above is the content of "how to use pointers and const modifiers". After the study of this article, I believe you have a deeper understanding of how pointers and const modifiers are used, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!