Analysis of constant pointer and pointer constant in C / C ++

    In C / C ++, const is often encountered to modify pointers, which leads to pointer constants and constant pointers. It is often messed up in use, and some sorting is done on this.

1. Constant pointer

       As the name implies, a constant pointer is essentially a pointer, but is decorated with a const constant, indicating that the pointer points to a constant (in fact, we will find that it can also point to a variable later). The meaning of pointing to a vector here is that you cannot modify the value of a variable through a pointer. Let's look at the following example:

int main(int argc, char *argv[])
{
	const int a = 10;
	int b = 20;
	const int* p_const;
	int *p_variable;
	p_const = &a; // const pointer can point to const or variable
	p_const = &b; // const pointer can point to const or variable
	//p_variable = &a;//[Error] error: invalid conversion from `const int*' to `int*'
	p_variable = &b;// Ok
	//*p_const =100;//[Error] error: assignment of read-only location
	*p_variable = 200;//OK, equal b =200
	b = 300;//b can be aassigned by variable b

	return 0;
}

 

       From the above results, we can draw conclusions: 1. The pointer is modified with const, which can point to a variable of type const or a normal variable that has been initialized. 2. A pointer modified with const cannot be used to modify the value of the variable pointed to by the pointer, but the value of the variable (normal variable after initialization) can be modified by the variable name defined by the variable. 3. A pointer modified with const can point to a variable modified by const or a variable that is not modified by const. However, pointers without const modification can only modify variables without const modification, so as a formal parameter in C / C ++, pointers of const type are often used. Because it can avoid modifying the value of the pointed variable.

      In short: The purpose of const modifying pointer variables is to prevent the value of variables from being changed by pointers.

2. Pointer constant

    Pointer constant means that the address stored in the pointer variable is a constant and cannot be modified. The form is generally written as: data type * const Pointer. The following is an example to illustrate:

int main(int argc, char *argv[])
{
	int a = 10;
	int b = 20;
	int * const const_pointer = &a; // value of const_pointer is a address
	int *pointer = &b;
	*const_pointer = 100;//ok equal a=100
	*pointer = 200; //ok equal b=200
	const_pointer = &b;//[Error] error: assignment of read-only variable `const_pointer'
	const_pointer ++;//[Error] error: assignment of read-only variable `const_pointer'
	pointer = &a;//OK, value of pointer is a address
	pointer ++;//OK, value of pointer increase by 1
		
	return 0;
}

       It can be concluded from the above example: 1. The address stored in the pointer constant cannot be changed, so the pointer cannot be assigned and other operations, but the variable pointed to by the pointer can be changed (constant pointer constant is not allowed).

      In short: pointer constants are similar to ordinary variable constants. The value he stores (for pointers is an address) cannot be changed once initialized.

 

Published 10 original articles · Like 11 · Visits 20,000+

Guess you like

Origin blog.csdn.net/u013323018/article/details/95947432