C-type delimiters const and volatile 1. Const In the Declaration, const usually appears after the storage type and before the data type. For example: Static const int A = 3; // A is an integer constant of the static storage type. Since the const has been used to limit the type of A, we can initialize a, but we cannot assign values to a, increment and reduce operations in the future. Although const is used to restrict variables, it is not allowed to describe the size of the array with variables in another declaration. Error Example 1: Const int K = 3; Int array [k]; // Error Therefore, variables restricted by const are not equivalent to symbolic constants. Error Example 2: The address of the Variable Limited by const is assigned to an unlimited pointer. Const int A = 7; Int * P = & A; // Error Cause of error: P is a common pointer to an int. Then we can use it in an expression like ++ * P, but it will change the value of, this is contrary to the probability of a constant.
Example 2: Const int A = 7; Const int * P = & A; // P is a pointer to an int-type constant. Its initial value is the address of.
Note: P itself is not a constant. We can assign values to it using some other addresses, but we cannot assign values to * P. We should not modify the object pointed to by P.
If we want to make P itself a constant rather than a, we can declare it as follows: Int; Int * const P = & A; // P is a constant pointer to an int. After that, we cannot assign values to P, but can assign values to * P.
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