C constant const

Source: Internet
Author: User


1. The value defined by const cannot be changed, similar to java final.
[Html]
# Include "stdio. h"
 
 
Main (){
Int const x = 15;
X = 16;
Printf ("% d \ n", x );
 
}

Output:
[Html]
Pateo @ pateo-B86N53X :~ /Work/study $ cc main. c-o main
Main. c: In function 'main ':
Main. c: 6: error: assignment of read-only variable 'X'

2. pointer Constants
[Html]
# Include "stdio. h"
 
 
Main (){
Int a = 3;
Int B = 6;
Int c = 9;
 
Int const * p = &;
Int * const p1 = & B;
Int const * const p2 = & c;
 
P = & B;
// * P = 4;/** error, error: assignment of read-only location '* P '**/
A = 4;
 
 
// P1 = & a;/** error, error: assignment of read-only location 'p1 '**/
* P1 = 5;
B = 7;
 
P2 = & c;/** error, error: assignment of read-only location 'p2 '**/
* P2 = 8;/** error, error: assignment of read-only location '* p2 '**/
C = 3;
 
}

Conclusion: in particular, we can basically understand the relationship between the const and the pointer from the column sub of the pointer constant.

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.