111-Analysis of C language const

const defines constant variables, read-only and cannot be written
1. The basic data type is transparent to const; const int ca = 10; equivalent to int const cb = 10;
2. In the pointer, const modifies its direct right side, and indirect is not counted .
transferred 3. permission, permission to transfer the same or reduced, but can not pass amplification
Conclusion : const in c language, the most important function value is not modified, parameter pointers preceded const

int main()
{
    
    
	int a = 10;
	int b = 20;
	const int ca = 10;//不能改
	int *p = &a;
	p = &b;
	//p = &ca;//error  假如合法  ,*p = 100;->ca=100
	const int *p2 = &ca;//ok
	//*p2 = 20;//error
	//int *const p3 = &ca;//error
	const int *p4 = &a;
	int *const p5 = &a;
	//p = p2;//error
	return 0;
}
//const 规则1
int main()
{
    
    
	int a = 10;//可读,可写
	printf("%d\n",a);//读
	a = 100;//写
	const int ca = 10;//常变量,只读
	printf("%d\n",ca);//读
	a = ca;//ca读
	//ca = 100;//error
	int const cb = 10;//完全等同ca
	//cb = 20;//error

	return 0;
}
//const 规则2
int main()
{
    
    
	int a = 10;
	int b = 20;
	int *p = &a;
	p = &b;//p,写
	*p = 100;//*p,写
	const int *cp1 = &a;
	//*cp1 = 100;//error
	cp1 = &b;//ok
	
	int const *cp2 = &a;//等同cp1(规则1)
	//*cp2 = 100;//error
	cp2 = &b;//ok

	int *const cp3 = &a;
	*cp3 = 100;//ok
	//cp3 = &b;//error
	
	const int *const cp4 = &a;//不使用
	//*cp4 = 100//error
	//cp4 = &b;//error
	
	 //a + b;

	return 0;
}

The const modified data type refers to the constant type, and the value of the constant type variable or object cannot be updated.
The original purpose of const was to replace precompiled instructions, eliminate its shortcomings, and inherit its advantages.

(1) You can define const constants, which are immutable.
  For example: const int Max=100; Max++ will produce errors;
(2) It is convenient for type checking, so that the compiler has a better understanding of the processing content and eliminates some hidden dangers.
  For example: void f(const int i) {…} The compiler will know that i is a constant and it is not allowed to be modified;
(3) It can avoid the appearance of ambiguous numbers, and it is also convenient to adjust and modify parameters. Like the macro definition, you can do the same, and you will change when you change!
(4) It can protect the modified things, prevent accidental modification, and enhance the robustness of the program. Still the above example, if i is modified in the function body, the compiler will report an error;
  for example: void f(const int i) {i=10;//error!}
(5) Can save space and avoid unnecessary memory distribution. For example:
Insert picture description here
(6) Improved efficiency.
  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.

The following code is not modified by const. It first assigns the address of n to the pointer p, and then dereferences p to 20; and then assigns the address of m to p. This code is fine.

#include <stdio.h>
void test1()
{
    
    
	int n = 10;
	int m = 20;
	int * p = &n;
	*p = 20;
	p = &m;
}
int main()
{
    
    
	test1();

	return 0;
}

When the const modification is on the left, that is: const modifies the content pointed to by the pointer variable, and it is not allowed to modify the pointer. If *p is dereferenced and assigned to 20, the compiler will report an error, but the content of the pointer can be changed, that is: change m When the address is given to the pointer p, the compiler will not report an error.
Insert picture description here
Insert picture description here
When the const modification is on the
right, that is: const modifies the pointer variable itself, the content of the pointer variable cannot be modified, that is: the address of m is given to the pointer p, the compiler will report an error, but the pointer The content of can be changed. If *p is dereferenced and assigned to 20, the compiler will report an error.
Insert picture description here
Insert picture description here
When const modifies a pointer variable:
1. If const is on the left side of *, it modifies the content pointed to by the pointer to ensure that the content pointed to by the pointer cannot be changed by the pointer. But the content of the pointer variable itself is variable.
2. If const is placed to the right of *, it modifies the pointer variable itself, ensuring that the content of the pointer variable cannot be modified, but the content pointed to by the pointer can be changed through the pointer.

If you
want to ensure that the content pointed to by the pointer cannot be modified, but also that the pointer variable itself is not modified, then use const to modify the content pointed to by the pointer and the pointer variable itself, as follows:
Insert picture description here

Guess you like

Origin blog.csdn.net/LINZEYU666/article/details/111566479