Explain the usage of const keyword in C language

  • 2020-04-02 03:15:00
  • OfStack

Const keyword is used to define constants, if a variable is const modifiers, then its value can't be changed, I think there is such a question, someone must have in the C language has a # define, isn't it, why use const, the existence of the things I think must have its own way, so the existence of the const must have its rationality, compared with the precompiled directives, The const modifier has the following advantages :

1. Precompiled instructions simply replace values and cannot be type-checked

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

3. The compiler usually does not allocate storage space for ordinary const constants, but stores them in symbol tables, which makes it a constant at compile time and efficient without having to store and read memory.

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

I. modification of 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 is important to note that when modifying the variable with const, the face change must be initialized, otherwise the value cannot be assigned later.

Now let's look at const for constant static strings, for example:

Const char * STR = "fdsafdsa";
Without the const, we might end up writing a statement like STR [4]='x', either intentionally or unintentionally, which would result in an assignment to a read-only memory area, and the program would immediately terminate abnormally. With const, this error can be detected immediately when the program is compiled, which is the benefit of const. Allow logical errors to be discovered at compile time.

Two, constant pointer and pointer constant

A constant pointer is a pointer to something that is a constant, which can be defined in one of two ways.

Const int * n.
Int const * n.
Here are two things to note:

1. Constant pointer says that you cannot change the value of a variable through this pointer, but you can still change the value of a variable through other references.

Int a = 5;
Const int * n = & a;
A = 6;
2, the value of the constant pointer can not be changed, but this does not mean that the pointer itself can not be changed, the constant pointer can point to other addresses.

Int a = 5;
Int b = 6;
Const int * n = & a;
N = & b;
Pointer constant refers to the pointer itself is a constant, can not point to other addresses, written as follows:

Int * const n;
Note that the address to which the pointer constant points cannot be changed, but the value held in the address can be changed by other Pointers to the changed address.

Int a = 5;
Int * p = & a;
Int * const n = & a;
* p = 8;
The key to distinguish constant pointer from 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 the const as a 'constant', the content fits. Int const * n; Int *const n; Is a pointer constant.

A constant pointer to a constant

A combination of the two, the position of the pointer cannot be changed and the value of the variable cannot be changed by this pointer, but the value of the variable can still be changed by other ordinary Pointers.

Const int * const p;
Third, modifies the function parameter

According to constant pointer and pointer constant, the parameter of const modifier is also divided into three cases

1, prevent the modification of the pointer to the content

Void StringCopy(char *strDestination, const char *strSource);
Where strSource is the input parameter and strDestination is the output parameter. After you const the strSource, the compiler will point out the error if the statement inside the function tries to change the contents of the strSource.

2, prevent the modification of the pointer to the address

Void swap (int * const p1, int * const p2)
The address to which pointer p1 and pointer p2 point cannot be modified.

3. Combination of the above two.

The return value of the modifier

If the function return value in "pointer pass" mode is modified with const, then the contents of the function return value (pointer) cannot be modified, and the return value can only be assigned to the same type of pointer with const modification.
Such as function

Const char * get string (void);
A compilation error will appear in the following statement:

Char * STR = get string ();
The correct usage is

Const char * STR = GetString();
Modify global variables

Global variable scope is the entire file, we should try to avoid using global variables, once thought have a function to change the global variable value, it will also affect the other references to the variable function, cause it's difficult to find, in addition to bug after if must use global variables, we should try to use the const modifier to the decoration, unnecessary thought to modify this way, using the method and the local variables are the same.

Above is the use of the const keyword all hope to help you use the const keyword flexible


Related articles: