Differences between c ++ and C const variables: const Variables

Source: Internet
Author: User

Differences between c ++ and C const variables: const Variables

Difference 1:

The const in C Language defines a const variable, which can only be read but not written.

The const of C ++ defines a constant.

Const int a = 5; int array [a]; // It is incorrect in C, because a read-only variable int array [a] is defined in C. // It is correct in c ++ because a constant is defined in C ++.

Difference 2:

Const functions cannot be defined in C language, while const functions can be defined in C ++.

Const member functions of C ++: the value of member variables of the class cannot be modified. (Examples are not listed here)

Pointer constants and constant pointers:

1. Constant pointing to the pointer: When the const is before the * sign, it is the pointer pointing to the constant.

Const char * p1 = "123456"; char const * p2 = "123456"; p1 = "werr"; // correct, you can point to another constant p1 [0] = 'a'; // error. The value pointing to the address cannot be modified.

2. Constant pointer: When the const is after the * sign

Char * const p1 = "12445"; p1 [0] = 'W'; // correct. You can modify the value pointing to the memory p1 = "ddddd"; // error, the memory to which p1 points cannot be modified.

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.