Detailed Explanation of C Language Keywords (4) Take you to a comprehensive understanding of the const keyword

I. Introduction

Hello everyone, welcome to the C language in-depth analysis column - the fourth part of the C language keyword detailed explanation, in this article we will introduce another important keyword const in the C language, this keyword is often asked in interviews I hope you can have a comprehensive understanding of it

2. The const keyword

1. General description of the const keyword

Const is the abbreviation of constant, which means constant and constant, and is also translated into constants, constants, etc. Unfortunately, because of this, many people think of const-modified values ​​as constants. This is imprecise, it should be a read-only variable precisely, its value cannot be used at compile time, because the compiler does not know what it stores at compile time.
The original purpose of the introduction of const is to replace the precompiled instruction, eliminate its shortcomings, and inherit its advantages (define is not a keyword).

2, const modified variable

const-modified variable: A variable modified with const is called a constant variable. It is said that const defines a variable, but it is equivalent to a constant; that it defines a constant, but has the attributes of a variable, so it is called a constant variable. Its function is to assign the read-only attribute to the variable, so that the variable cannot be directly modified.
Here's an example to illustrate:
insert image description hereinsert image description hereinsert image description here

Here are three questions

The first question: how to understand the word "direct" in the variable that is modified by const cannot be directly modified?

From the above, we can observe that the compiler will report an error when a variable modified by const is directly modified, but we can still indirectly modify the value of the variable by means of pointers, that is, the value of the variable cannot be modified by const modified variables This property is not absolute, it is only a compiler-level guarantee.

The second question: how can we really ensure that variables are not modified?

Here's an example.
insert image description hereinsert image description hereinsert image description hereHere, we define a constant string and try to change the first character h in it to an uppercase H. We can see that the program does not report any errors during compilation, but after running, we find that the program crashes , then we enter debugging, execute *p = 'H', and find that the conflict of write permissions is prompted. This is a strong constraint at the operating system level, which guarantees that variables cannot be modified in the true sense.

The third question: Since const does not really guarantee that the variable will not be modified, why use it?

There are two reasons:
1. It allows the compiler to directly check the syntax of the variables modified with const when compiling the code, so that we can discover in advance the errors caused by the direct modification of some variables that should not be directly modified ( maximum effect).
2. Make the variable self-descriptive (equivalent to telling other programmers not to modify the variable directly).

3, const modified array

const modifies an array and assigns a read-only attribute to the array, so that each element in the array cannot be directly modified (read-only array). (same as const-modified variables)insert image description hereinsert image description hereinsert image description here

4, const modified pointer

First of all, we need to understand what is the variable pointed to by the pointer and what the pointer points to.

#include<stdio.h>
int main()
{
    
    
	int a = 10;
	int * p = &a;       //此时指针p指向a
	*p = 100;           //修改指针指向的变量,即a
	p = 0x11223344;     //修改指针的指向,使p指向另一块空间
	printf("%d\n", a);
	return 0;
}

insert image description here

Then, let's explore the different cases of const-modified pointers

1. const is placed before the type name (int*): it means that the variable pointed to by the pointer cannot be directly modified, and the pointer pointed to can be modified.
insert image description hereinsert image description hereFrom the above figure, we can see that when const is placed in front of the type, we can directly modify the pointer of the pointer variable p, but we cannot directly modify the variable a pointed to by p.
Note: 1. const is placed in front of int and behind int. is placed in front of the type (int*), so the effect of the two is exactly the same, but it is recommended to put const in front of int.
2. The warning here is because p is a pointer variable, which stores the address of a. Now I directly assign another address to p. Since the address is also a data, it is regarded as an integer, and a type mismatch occurs. .

2. const is placed after the type name (int*): it means that the pointer to which the pointer points cannot be modified directly, and the variable pointed to by the pointer can be modified directly.
insert image description hereinsert image description hereSimilarly, we can see that when const is placed after the type name, the content pointed to by p can be directly modified, but the pointer of p cannot be directly modified. (The warning here is for the same reason as above)

3. const is placed before and after the type name (int*) at the same time: neither the variable pointed to by the pointer nor the pointer pointed to by the pointer can be directly modified.
insert image description here

5, const modified function

1. const modified function parameter: It means that the parameter cannot be directly modified within its function. insert image description hereinsert image description here
Here we define a variable a, and then define a pointer variable p to store the address of a, and then pass p to the test function, and modify the value of a through the pointer inside the test function. There is no problem, but when we test When a function's parameters are preceded by const, we find that a can't be modified directly in the same way.

2. Const modified return value: Indicates that the return value of the function cannot be modified directly.
insert image description hereinsert image description hereThe principle of modifying parameters here is actually the same as the above, so I won’t repeat them, but it should be noted that here I use static to modify the local variable a. The reason is that a is a local variable, and the variable is called when entering the test function. Create, out of the test function, the variable is destroyed, but here we return the address of a to the place where test is called, indicating that we are likely to access a through the address of a in the future, so in order to avoid illegal access or access data later If it is lost, we use static to decorate it to extend its life cycle, so that the a variable is valid during the entire program running (the specific usage and details of static are described in my keyword detailed explanation II).

3. Summary

  • const modified variable: assign a read-only attribute to the variable, so that the variable cannot be directly modified.
  • const modified array: assign the read-only attribute to the array, so that each element in the array cannot be directly modified.
  • const modifies a pointer:
    const is placed before the type name (int*): it means that the variable pointed to by the pointer cannot be directly modified, and the pointer pointed to can be modified.
    const is placed after the type name (int*): it means that the pointer pointed to cannot be directly modified, and the variable pointed to by the pointer can be modified directly.
    const is placed both before and after the type name (int*): neither the variable pointed to by the pointer nor the pointer pointed to can be directly modified.
  • const-modified function:
    The const-modified function parameter means that the parameter cannot be directly modified within its function.
    : const Modified return value: Indicates that the return value of the function cannot be directly modified.
  • How to understand "direct": The property of const-modified variables so that the value of the variable cannot be modified is not absolute, it is only a guarantee at the compiler level, and we can still modify it through indirection, such as pointers.
  • Why use const:
  • It allows the compiler to directly check the syntax of variables modified with const when compiling the code, so that we can find errors caused by direct modification of some variables that should not be directly modified (the greatest effect).
  • Make the variable self-descriptive (equivalent to telling other programmers not to modify the variable directly).

More keywords are in the blog link below.
Detailed explanation of C language keywords (1) auto, register keyword
detailed explanation of C language keywords (2) to give you a comprehensive understanding of static
C language keywords (3) data types and sizeof keywords

If you think this article is helpful to you, please give a triple support

Guess you like

Origin blog.csdn.net/m0_62391199/article/details/123686916