Use of the const qualifier

Source: Internet
Author: User

In the program, for data, we have a kind of data that we want to be given, and we don't change it. The book often takes PI for example!

We put this type of data at the beginning of the given data and are called constants when the program is running without changing it.

So here it is, if you want to use a const, then you have to make sure that the number is a constant.

1th: Since it is the same, and we know its value, then we have to initialize it. Can't change it later!

2nd: A constant has a value in memory, and it can be interpreted as a brace in the presence of the scope in which it resides.

3rd: const Modifies what, what is a constant (below will combine some code, give several constant types)

#include "stdafx.h" #include <iostream>

int _tmain (int argc, _tchar* argv[]) {

//const int A;

//error, to declare a constant a, but uninitialized, constants must be initialized at the beginning.

const int a = 10;

Correctly, a constant A is declared and initialized to 10.

a=20;

///error, attempting to give 20 to constant a, at which point A is already a constant and cannot be changed in the program

const int *p;

Right, a pointer p is declared, and the pointer is pointing to a constant

The right side of the const is the object it modifies directly, that is, the int type.

p = &a;

Correct, the address of A to the pointer P, note that at this point p is a pointer variable, p can be arbitrarily changed

int B = 10;

Defines a variable B, initialized to 10;

p = &b;

Correct, the pointer p re-points to another address.

int const *P2;

Correctly, a pointer to a constant is defined, the same as the const int *P written above.

//int * Const P3;

Error, a constant pointer is defined, which means that the pointer itself is a constant, which is both a constant and an initialization.

int * Const P3 = &b;

correctly, initializes a constant pointer

//P3 = &a;

Error, attempting to change the direction of the P3, at which point the P3 is a pointer constant and is not allowed to change.

*P3 = 10;

Correct, change the value that the P3 points to the space, notice, at this time P3 or P3, just P3 corresponding space in the number of changes.

That's the main part.

System ("pause");

return 0;

}

Use of the const qualifier

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.