Const keyword and pointer, const keyword pointer

Source: Internet
Author: User

Const keyword and pointer, const keyword pointer

Four cases of const and pointer combination

The four situations are as follows:

Int cons * p itself is not a constant, and p points to the address of the data is a constant

Const int * p itself is not a constant, and p points to the address of the data is a constant

Int * const p is a constant, and p points to an address space that is not a constant.

Const int * const p is a constant, and the address space pointed to by p is also a constant

 

Summary: When const is on the left side of *, it modifies the data of the address pointed by p. When const is on the right side of *, the modified p

 

When a variable is modified with const, the value of the variable can still be modified with the pointer in gcc.

Int main (void)

{
Const int a = 5;
// A = 6; // error: assignment of read-only variable 'A'
Int * p;
P = (int *) & a; // The alert height here can be eliminated by force type conversion
* P = 6;
Printf ("a = % d. \ n", a); // a = 6, the result shows that the const type variable is changed

Return 0;

}

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.