C language beginner-const pointer

Source: Internet
Author: User

Note: initialization is required during the declaration.
Declaration method: type name * const variable name
1. You cannot point to another variable. The value of the pointer cannot be changed.
[Cpp]
# Include <stdio. h>
Void main (){
Int a = 89;
Int * const p = NULL;
P = &;
 
}

 
Error:
Error C2166: l-value specifies const object
2. assign values to the variable through the pointer (the variable value is not const)
[Cpp]
# Include <stdio. h>
Void main (){
Int a = 89;
Int * const cp = &;
* Cp = 188;
Printf ("a = % d \ n", );
}

 
Result:
A = 188
Press any key to continue
3. the const Pointer Points to the const variable and changes the value of the variable (which can be implemented in LINIX)
[Cpp]
# Include <stdio. h>
Void main (){
Const int B = 89;
Int * const pp = & B;
* Pp = 100;
Printf ("B = % d \ n", B );
}

Method 2: const type name * variable name
1. pointer to constants pointer to a constant
Pointer to a constant to avoid modifying the value of a variable, regardless of whether the variable is a const variable.
[Cpp]
# Include <stdio. h>
Void main (){
Int x = 123;
Const int * pc = & x;
* Pc = 8989;
Printf ("% d \ n", x );
}

 
Result: it cannot be modified.
Rror C2166: l-value specifies const object
2. pointer to a constant. The pointer can point to another variable, and the value of the pointer can be changed.
[Cpp]
Void main (){
Int x = 123;
Int y= 321;
Const int * pc = & x;
Printf ("% p \ n", pc );
Pc = & y;
Printf ("% p \ n", pc );
}

 
3. A pointer to a constant cannot be assigned as a normal pointer, because a pointer to a constant cannot modify the value of a variable. If you assign it to a normal pointer, then www.2cto.com
It is not allowed to modify the value of a variable.
[Cpp]
# Include <stdio. h>
Void main (){
Int x = 123;
Const int * pc = & x;
Int * q = pc;
}

 
Error:
Error C2440: 'initializing': cannot convert from 'const int * 'to 'int *' Conversion loses qualifiers
 
Method 3: const int * const variable name
The value of the pointer variable and the value of the space pointed to by the pointer cannot be changed.
 
 
 
Deepen:
[Cpp]
# Include <stdio. h>
Void main (){
// The first basic type of const variable, where the const position can be
Const int x = 123;
Const int y = 321;
// Define a non-constbianl
Int z = 111;
// Define a pointer to a constant
Const int * p = & x;
// Define a constant pointer
Int * const q = & z;
// Define a non-const pointer
Int * w = & z;
 
X = y; // It is incorrect because the const variable cannot be modified, and the left value of the const variable cannot be used.
P = & z; // correct. The value of the pointer to a constant can be modified, that is, it can point to another variable, however, you cannot use this pointer to modify the memory space it points.
 
Value
* P = 10; // error. The pointer to a constant cannot be used to modify the value of a variable.
* P = y; // error, same as above
P = w; // correct. The value of the pointer to the constant can be modified.
Z = 5; // correct. z is a variable that can be changed.
Z = x; // correct. x can only be used as the right value as the const variable, not the left value.
* Q = x; // correct. The const pointer variable can be used to modify the value in the address to which it points.
Q = & x; // error, because x is a constant here and cannot be changed. The q pointer can be used to change the value of a variable.
// However, the principle is acceptable, and the value of the variable can be changed in Standard c.
W = & x; // error. Because x is a constant here, it cannot be changed, and w is a normal pointer that can be used to change the value of a variable.
// However, the principle is acceptable, and the value of the variable can be changed in Standard c.
W = p; // error, because the pointer to a constant cannot change the value in the memory space it points, the common pointer w can be used to change the value of a variable.
 
You cannot assign a pointer to a constant to a normal pointer.
W = q; // correct, because the q pointer can be used to change the value of the variable, and the w normal pointer can also be used.
 

}


From like7xiaoben

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.