Const keywords and pointers

Source: Internet
Author: User

Four situations in which const and pointers are combined

The four cases were as follows:

The int cons *p p itself is not a constant, and the data for the address that P points to is constant

the const int *P p itself is not a constant, and the data for the address pointed to by P is constant

int * Const P p is constant, p is not constant in the address space pointed to

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

Summary: When the const is on the left side, the data of the address that the P points to is modified, and when the const is on the right, the modified P

When a variable is modified with a const, the value of the variable can still be modified by the pointer under GCC

   int main (void)

{
const int a = 5;
A = 6;//error:assignment of read-only variable ' a '
int *p;
p = (int *) &a;//here Alarm high can be eliminated by forcing type conversion
*p = 6;
printf ("A =%d.\n", a);//A = 6, the result proves that the variable of the const type has been changed

return 0;

}    

Const keywords and pointers

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.