The basic function and usage of the Const keyword in c language

Source: Internet
Author: User

You see the const keyword, most people will not think it is a const constant, I tell you that this is wrong cognition, then what is the actual meaning of it? In fact, a const cannot change a variable to a constant, but rather a variable into a read-only variable, so we cannot modify its value directly, but we can modify its value by its address.

1. General usage of const

const int n = 5;

int const n = 5; The effect of these two sentences is the same, it is declared a read-only variable named a, we can not directly modify the value of a.

int A[n] = {1, 2, 3, 4, 5}; The size of the required array is a constant, that is, a definite value, and n is a read-only variable, which means that there is a definite value when n is initialized, so it can be used as the size of the array

2. Const for pointers

int a = 5;

int B = 10;

const INT * p = &a;

int const * p = &a; The function of these two sentences is the same, it is declared a constant pointer called p, that is, p is the corresponding storage space is read-only, we can not change the content by P

p = &b; The correct wording

*p = 20; Wrong notation, because the const modifier is *p, so *p is read-only, and we can't write to read-only storage (p points to B is a variable, not a constant, but that's not possible)

b = 20; Correct notation, because B is a variable, no const modifier b

---------------------------------------------------------------------------------------

int * Const Q = &a; Function: const is on the right side of *, declaring q is a pointer constant

Q = &b; Wrong notation, q is a read-only pointer variable and we cannot reassign it

*q = 20; Correct notation, because there is no const modification before *q, so Q point to the storage space is still readable, so *q is a variable, not a constant, we can modify the corresponding storage space by q

b = 20; The correct wording

We have to look at the location of the const is different, so play a different role, we have to think more. Summary: 1. Const on the left side, you can not change the *p, that is, can not be assigned: *p = 20; 2. Const on the right side of the *, you cannot change the p, that is, you cannot assign this value: P = &b;

3. Some of the cases of const:

1)

int main () {

const int a = 5;

int * p = (int *) &a;

*p = 10;

printf ("%d\n", a);

printf ("%d\n", *p);

}

We found out that a is a value of 5, and the value of *p is 10. Why is there such a situation? is storage space storing a different two values? Of course not, because when the compiler is preprocessing, only the const-modified variable value is read once, so print a is 5, actually we change the value of a by the pointer variable p, so the value of a is actually 10. Summary: 1. A const-Modified local variable is stored in the stack, and we can modify its value by pointer. 2. The compiler only reads the value of a const-modified variable once, at the time of preprocessing. 3. If a is a const-modified global variable, then this is not possible, because its value is stored in the global storage space, its value is only a readable property, cannot be modified.

In other words, it is not possible to write the following:

const int a = 5;

int main () {
int * p = (int *) &a;

*p = 10; It is wrong to write this way.

printf ("%d\n", a);

printf ("%d\n", *p);

}

2)

int a = 5;

int B = 10;

const INT * Const P = &a; Both the pointer itself and the storage space pointed to by the pointer are read-only.

p = &b; Wrong notation, because P is preceded by a const modifier, so p is a pointer constant, which means that the value of P cannot be re-assigned.

*p = 20; Wrong notation because *p has a const modifier in front of it, so *p is a constant, that is, p is a constant pointer, and the storage space it points to is read-only.

OK, the basic use of Const said here, we can go back to compare, serious thinking, I believe there will be no small harvest ...

The basic function and usage of the Const keyword in c language

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.