Summary of the magical use of const keyword in C language

After learning the C language for many years, have you mastered all the use of the const keyword? Are you confused about the use of const in programming practice? Today, I will discuss the summary for everyone together, and hope that everyone can solve their doubts and provide references.

The const keyword in C language is the abbreviation of constant, which is usually translated into constants, constants, etc. Some friends immediately think of constants as soon as they see the const keyword. In fact, the const function in C language is very powerful, it can modify variables, arrays, pointers, function parameters, etc. This article will give a detailed summary of the above-mentioned modification functions.

1. Modified variables:

The C language uses const to modify variables. The function is to declare the variable as a read-only feature and protect the value of the variable from being modified. Examples are as follows:

const int i = 5;

The above example shows that the variable i has read-only characteristics and cannot be changed; if you want to re-assign i, such as i = 10; it is wrong.

It is worth noting that while defining variables, they must be initialized. The definition form can also be written as int const i=5, which is also correct.

In addition, const modified variables also serve the purpose of saving space. Usually, the compiler does not allocate space for ordinary const read-only variables, but saves them in the symbol table, without reading and writing memory operations, and the efficiency of program execution will also be improved.

2. Modify the array

In C language, const can also modify arrays. Examples are as follows:

const int array[5] = {1,2,3,4,5};

array[0] = array[0]+1; //错误

Array elements are similar to variables, they have read-only properties and cannot be changed; once they are changed, the program will report an error.

3. Modified pointer

Special attention should be paid to const modified pointers in C language. There are two forms, one is used to limit the value of the space pointing to the space cannot be modified; the other is to limit the pointer cannot be changed. Examples are as follows:

int i = 5;

int j = 6;

int k = 7;

const int * p1 = &i; //定义1

int * const p2 =&j; //定义2

Two pointers p1 and p2 are defined above.

In Definition 1, const restricts *p1, that is, the value of its pointing space cannot be changed. If the value of its pointing space is changed, such as *p1=20, the program will report an error; but the value of p1 can be changed. Assignment such as p1=&k is no problem.

In Definition 2, const defines the pointer p2. If you change the value of p2, such as p2=&k, the program will report an error; but *p2, that is, the value of the space it points to can be changed. For example, *p2=80 is no problem. The program executes normally.

4. Modify function parameters

The const keyword modifies function parameters, restricts the parameters and prevents them from being modified within the function. The defined function parameters can be ordinary variables or pointer variables. Examples are as follows

void fun1(const int i)

{

其它语句

……

i++; //对i的值进行了修改,程序报错

其它语句

}

void fun2(const int *p)

{

其它语句

……

(*p)++; //对p指向空间的值进行了修改,程序报错

其它语句

}

 

 

Undertake programming in Matlab, Python and C++, machine learning, computer vision theory implementation and guidance, both undergraduate and master's degrees, salted fish trading, professional answers please go to know, please contact QQ number 757160542 for details, if you are the one.

 

Guess you like

Origin blog.csdn.net/weixin_36670529/article/details/115053457
Recommended