C language 丨 const keyword usage detailed explanation

This article mainly analyzes and introduces the usage of the const keyword in the C language. The sample code in the article is very detailed and has a certain reference value. Interested friends can refer to it.

 

What is const? 

const is not used much in actual programming, const is the abbreviation of constant, which means "constant"! It is a keyword to define read-only variables, or const is a keyword to define constant variables.

It is said that const defines a variable, but it is equivalent to a constant; it is said that it defines a constant but has the properties of a variable, so it is called a constant variable. The method of defining constant variables with const is very simple, just add const before the usual definition of variables, such as:

const int a=10;

const and variable type int can interchange positions, the two are equivalent, that is, the last statement is equivalent to:

int const a=10;

The keyword const is used to define constants. If a variable is modified by const, its value cannot be changed. I think someone must have such a question. Isn’t there #define in C language, why use const? , I think the existence of things must have its own reason, so the existence of const must have its rationality.

Compared with precompiled instructions, const modifier has the following advantages:

1. The pre-compiled instructions only simply replace the value, and cannot perform type checking

2. It can protect the modified things, prevent accidental modification, and enhance the robustness of the program

3. The compiler usually does not allocate storage space for ordinary const constants, but saves them in the symbol table, which makes it a constant during compilation, without the operation of storing and reading memory, making it highly efficient .

Let's talk about the usage of const from several aspects:

One, modify local variables

const int n=5;

int const n=5;

These two ways of writing are the same, they both mean that the value of the variable n cannot be changed. It should be noted that when using const to modify the variable, the face change must be initialized, otherwise the assignment will not be possible afterwards.

Next look at const used to modify constant static strings, for example:

const char* str="fdsafdsa";

If there is no const modification, we may write str[4]='x' statement later, intentionally or unintentionally, which will cause the assignment of the read-only memory area, and then the program will immediately terminate abnormally. With const, this error can be checked immediately when the program is compiled, which is the advantage of const. Let logic errors be discovered at compile time.

Two, constant pointer and pointer constant

A constant pointer means that the content pointed to by a pointer is a constant, which can be defined in the following two ways.

const int* n;

int const* n;

There are two points to note:

1. The constant pointer says that the value of the variable cannot be changed through this pointer, but the value of the variable can still be changed through other references.

int a=5;

const int* n=&a;

a=6;

2. The value pointed to by the constant pointer cannot be changed, but this does not mean that the pointer itself cannot be changed, and the constant pointer can point to other addresses.

int a=5;

int b=6;

const int* n=&a;

n=&b;

Pointer constant means that the pointer itself is a constant and cannot point to other addresses. It is written as follows:

int*const n;

It should be noted that the address pointed to by the pointer constant cannot be changed, but the value stored in the address can be changed and can be modified by other pointers to the changed address.

int a=5;

int*p=&a;

int* const n=&a;

*p=8;

The key to distinguish between constant pointer and pointer constant is the position of the asterisk. We use the asterisk as the dividing line. If const is to the left of the asterisk, it is a constant pointer, and if const is to the right of the asterisk, it is a pointer constant. If we read the asterisk as a "pointer" and const as a "constant", the content fits exactly. int const * n; is a constant pointer, int *const n; is a pointer constant.

Constant pointer to constant

It is a combination of the above two, the position pointed to by the pointer cannot be changed and the value of the variable cannot be changed through this pointer, but the value of the variable can still be changed through other ordinary pointers.

 const int* const p;

Three, modify the parameters of the function

According to constant pointer and pointer constant, the parameters of const modified function are also divided into three situations

1. Prevent modifying the content pointed to by the pointer

void StringCopy(char*strDestination, const char*strSource);

Where strSource is an input parameter and strDestination is an output parameter. After adding const to strSource, if the statement in the function body tries to change the content of strSource, the compiler will indicate an error.

2. Prevent modification of the address pointed to by the pointer

void swap ( int* const p1 , int* const p2 )

The addresses pointed to by pointer p1 and pointer p2 cannot be modified.

3. A combination of the above two.

Fourth, modify the return value of the function

If const is added to the return value of a function in the "pointer passing" mode, the content of the function return value (pointer) cannot be modified, and the return value can only be assigned to a pointer of the same type with const modification. 

For example function

const char* GetString(void);

The following statement will cause a compilation error:

char*str = GetString();

The correct usage is

const char*str = GetString();

Five, modify global variables

The scope of global variables is the entire file. We should try to avoid using global variables, thinking that once a function changes the value of a global variable, it will also affect other functions that refer to this variable, making it difficult to find out except for bugs. We must use global variables. We should try our best to use the const modifier to modify, so that the modification is unnecessary, and the method used is the same as that of local variables.

The above is all the usage of the const keyword. I hope it will be helpful for everyone to use the const keyword flexibly.

 

Finally, if you also want to become a programmer and want to quickly master programming, quickly join the learning penguin circle !

There are senior professional software development engineers who can answer all your doubts online ~ Introduction to programming language "so easy"

Programming learning books:

 

Programming learning video:

 

Guess you like

Origin blog.csdn.net/Hsuesh/article/details/112564860
Recommended