c and c ++ language in the const

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/shunli008/article/details/78823593

Look at the following ## questions

About const usage, the following error is

A	int const a = 3; int *p = &a;
B	int a = 3; int const *p = &a;
C	int a = 3; int * const p = &a;
D	const int a = 3; int const &b = a;

Which to choose?
This is when I learned c ++ is a question that only C language may not know much about the d option.

Here are some c language (c ++) in the const

const is a keyword, which defines a variable is not allowed to be changed, resulting in a static role, which is equivalent to a variable plus permission, into a read-only variable, but can not modify its value.

Release const initial purpose is to replace the pre-compiler directive, eliminating its disadvantages, while inheriting its advantages. (Baidu Encyclopedia)

The following describes the specific usage const Slitting

1. Modified ordinary variables

const int x = 3;

const int type x is modified into a read-only variable (say has become a constant on some video, I think this statement is not accurate), its value can only be read, but can not be changed, E.g

const int x = 3;
x = 5;//这是错误的

The wording in the compiler will display an error (different compilers prompt different)
error: the Assignment of the Read-only variable 'the X-'
This also confirms the above statement, it is a read-only variable, const.

Guess you like

Origin blog.csdn.net/shunli008/article/details/78823593