Duanxx C + + Learning: Const pointers for specific explanations

Source: Internet
Author: User

Const refers to a compile-time constant.

Keywordconst allows the code to determine whether a variable can be altered.

With const, you can prevent changes to variables or pointers, and more importantly, a const reference prevents changes to the referenced object .

In general, in C, I habitually use define for the definition of some constants, whereas in C + + it is better to use const instead.

For embedded programs, the use of const is quite subtle, and it is important that the variables are stored in ROM after being modified by Const.

With regard to the use of const pointers, there is no meaning in the text explanation, directly refer to the following code and gaze:

#include <iostream>//const//-A compile time constraint that an object can is not modifiedint main () {const int i = 1;int a = 0;const int *p1 = &i;///< data is const and pointer is notint* const P2 = &a;    < pointer p2 itself is a const, but the data P2 point to was not const//int* const P3 = &i;     < illegal, cannot convert from ' const int * ' to ' int *const ' int const* p4 = p1;      < If Const is on the left of *,data is const///< if cosnt are on the right of *,pointer are constconst int* const p 5 = &i; <the data and the pointer are both Constreturn 0;}

Duanxx C + + Learning: Const pointers for specific explanations

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.