C language's const keyword

Source: Internet
Author: User

The Const keyword is common in C code, and the following summarizes its role:

1. Declare a variable as constant, which is read-only. This is similar to # define, such as: const int A;

At this time, a is considered a constant, with an immutable nature. The problem with this statement is that there is no initialization for a, the compiler assigns a space to a, what values it used to be, what value is the default, so you will be given an error after assigning a value.

The appropriate declaration should be this: const int a = 3;

That is, the declaration is initialized at the same time. It should be noted that the int const A = 3; The effect is the same, in which case the const and the position of the data type can be reversed.


2. Modifier pointer variable

Introduce a little bit of pointers. The pointer is the address, and the so-called address is a string of numbers, but this number indicates a memory unit. Be aware that each storage unit in memory is numbered. Of course, the contents of a storage unit are essentially a bunch of numbers, but it's important to understand that the number and content of the cells are not the same thing, or that it doesn't matter, because they are interpreted differently.

Because the number representing the address is too long to remember, the C language uses the variable name instead of the number. It can be known that at compile time, each variable name is eventually replaced with a "digital address", that is, the variable name and the address of a storage unit is bound, corresponding.

Look first: int *const A; Then a is a variable that holds the data of a pointer type, that is, the address value. The storage unit represented by this address value holds an int type of data. Now A is modified by the const, so something cannot be changed. So in the end is the address stored in a can not be modified or the address of the corresponding storage unit inside the data can not be changed? The answer is the former, that is, a can only be stored in an address, can not be changed, only with the address of what value does not matter, casually changed. In code, a cannot give another address value, but *a can be arbitrarily assigned. A and *a each represent what everybody is very clear.

Second case: const int *a; This time, can not change is not the address value in a, but the content of this particular address can not be changed, and the previous situation is the opposite. Therefore, a can be given another address value, but the value of a does not change before the *a is not changed. (Note: The int const *A is the same as this declaration effect, so the essential difference is whether the * number is next to the variable or const)

Third case: const int *const A; Obviously, no one can change.


3. Modifier function Parameters
This is often seen in the case where the function's formal parameter is an array or pointer, in order to prevent the function from modifying the contents of the array passed over or the contents of the pointer (the second case in 2) was accidentally modified, and then added a const-qualified, such as: int findnum (const int array[], int num, int Conut);

4, to be supplemented.

C Language const keyword

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.