Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - Pointers and constant integers
Pointers and constant integers
const is right to interpret it as read-only. It is different from the usual constant. Although most books call it that.

in addition, the use of const in C and c++ is also slightly different. For example,

const int size = 1;

int a[size];

compiled in c, it will tell you that array a expects a constant length.

but it is ok to compile in C++.

But it was originally produced by C. const was invented in C only to express the read-only property of a variable and tell the coder or reader that this variable is read-only and has nothing to do with constants. This is the original intention.

as for what you said. const int * limitp;

firstly, according to the left binding property of const, it will preferentially bind and modify the left one, and only modify the closest one on the right. So it modifies int.

So: const int *limitp; int const *limitp; These two have the same function.

a pointer is declared, and the int pointed by this pointer is read-only.

Therefore, this pointer can be changed, but the pointed int cannot be modified by the pointer.

Therefore, if there is an operation like *limitp=12 to modify the pointer to the memory, an error will be reported.

this limitp =&; i; The writing is correct, and it is often used in C language.

because limitp is a pointer. & I is also equivalent to a pointer.

therefore, it is completely assignable. Const only shows that you can't change the value it points to by changing the pointer.

does not mean that the variable it points to cannot be modified elsewhere.

#include "stdio.h"

int main()

{

int i = 1;

const int *p = & i;

printf("%d\n",*p);

i = 8;

printf("%d\n",*p);

return ;

}

limitp=& i; This writing must be correct, because it is the assignment of a pointer.