很多人都答对了,我会在这里整理好并添加一些在给定答案中缺少的额外信息。
Const 是 C 语言中的关键字,也称为限定符。常量可以
应用于任何变量的声明以指定它的值
不会改变
-
const int a=3,b;
a=4; // give error
b=5; // give error as b is also const int
you have to intialize while declaring itself as no way to assign
it afterwards.
如何阅读?
只需从右到左阅读每条语句即可顺利运行
三个主要的事情
type a. p is ptr to const int
type b. p is const ptr to int
type c. p is const ptr to const int
[错误]
if * comes before int
两种
1. const int *
2. const const int *
我们先看
主要类型 1. const int*
在 3 个地方安排 3 件事情的方法 3!=6
我。 * 在开始时
*const int p [Error]
*int const p [Error]
ii. const 开始
const int *p type a. p is ptr to const int
const *int p [Error]
iii. int 开始
int const *p type a.
int * const p type b. p is const ptr to int
主要类型2. const const int*
在 2 个相似的 4 个地方安排 4 个东西的方法 4!/2!=12
我。 * 在开始时
* int const const p [Error]
* const int const p [Error]
* const const int p [Error]
ii. int 开始
int const const *p type a. p is ptr to const int
int const * const p type c. p is const ptr to const int
int * const const p type b. p is const ptr to int
iii. const 开始
const const int *p type a.
const const * int p [Error]
const int const *p type a.
const int * const p type c.
const * int const p [Error]
const * const int p [Error]
合二为一
输入一个。 p 是指向 const int (5)
const int *p
int const *p
int const const *p
const const int *p
const int const *p
输入 b。 p 是 const ptr 到 int (2)
int * const p
int * const const p;
键入 c。 p 是 const ptr 到 const int (2)
int const * const p
const int * const p
只是简单的计算
1. const int * p total arrangemets (6) [Errors] (3)
2. const const int * p total arrangemets (12) [Errors] (6)
一点额外的
int const * p,p2 ;
here p is ptr to const int (type a.)
but p2 is just const int please note that it is not ptr
int * const p,p2 ;
similarly
here p is const ptr to int (type b.)
but p2 is just int not even cost int
int const * const p,p2 ;
here p is const ptr to const int (type c.)
but p2 is just const int.
完成