我总是搞砸如何正确使用const int*、const int*const和int const*。有没有一套规则来定义你能做什么和不能做什么?

我想知道在赋值、传递给函数等方面的所有注意事项。


当前回答

要简单地记住:

若const在*之前,则值为常量。

如果const在*之后,则地址为常量。

如果const在*之前和之后都可用,则值和地址都是常量。

e.g.

int*常量变量//这里地址是恒定的。int常量*var//这里的值是恒定的。int常量*常量变量;//值和地址都是常量。

其他回答

就像每个人都指出的那样:

常量X*p、X*constp和常量X*const p之间有什么区别?

必须读取指针声明从右到左。const X*p表示“p指向常量的X”:X对象不能通过p更改。X*const p表示“p是指向非常量X的常量指针”:不能更改指针p本身,但可以通过p更改X对象。const X*const p表示“p是指向常量X的常量指针”:不能更改指针p本身,也不能通过p更改X对象。

我在下面画了一张图片来解释这一点,也许会有所帮助。

int const v和const int v是相同的。

最初的设计者多次将C和C++声明语法描述为失败的实验。

相反,让我们将类型命名为“pointer to type”;我叫它Ptr_:

template< class Type >
using Ptr_ = Type*;

现在Ptr_<char>是一个指向char的指针。

Ptr_<const char>是指向const char的指针。

const Ptr_<const char>是指向const char的const指针。

为了C遵循其他解释的完整性,对C++不确定。

pp-指针到指针p-指针数据-在示例x中指出的内容粗体-只读变量

指针

p数据-int*p;p数据-int常量*p;p数据-int*const p;p数据-int const*const p;

指向指针的指针

pp p数据-int**pp;pp p数据-int**const pp;pp p数据-int*const*pp;pp p数据-int常量**pp;pp p数据-int*const*const pp;pp p数据-int const**const pp;pp p数据-int const*const*pp;pp p数据-int常量*常量*常量pp;

// Example 1
int x;
x = 10;
int *p = NULL;
p = &x;
int **pp = NULL;
pp = &p;
printf("%d\n", **pp);

// Example 2
int x;
x = 10;
int *p = NULL;
p = &x;
int ** const pp = &p; // Definition must happen during declaration
printf("%d\n", **pp);

// Example 3
int x;
x = 10;
int * const p = &x; // Definition must happen during declaration
int * const *pp = NULL;
pp = &p;
printf("%d\n", **pp);

// Example 4
int const x = 10; // Definition must happen during declaration
int const * p = NULL;
p = &x;
int const **pp = NULL;
pp = &p;
printf("%d\n", **pp);

// Example 5
int x;
x = 10;
int * const p = &x; // Definition must happen during declaration
int * const * const pp = &p; // Definition must happen during declaration
printf("%d\n", **pp);

// Example 6
int const x = 10; // Definition must happen during declaration
int const *p = NULL;
p = &x;
int const ** const pp = &p; // Definition must happen during declaration
printf("%d\n", **pp);

// Example 7
int const x = 10; // Definition must happen during declaration
int const * const p = &x; // Definition must happen during declaration
int const * const *pp = NULL;
pp = &p;
printf("%d\n", **pp);

// Example 8
int const x = 10; // Definition must happen during declaration
int const * const p = &x; // Definition must happen during declaration
int const * const * const pp = &p; // Definition must happen during declaration
printf("%d\n", **pp);

N级取消引用

继续前进,但愿人类将你逐出教会。

int x = 10;
int *p = &x;
int **pp = &p;
int ***ppp = &pp;
int ****pppp = &ppp;

printf("%d \n", ****pppp);

很多人都答对了,我会在这里整理好,并在给出的答案中添加一些缺失的额外信息。

Const是C语言中的关键字,也称为限定符。Const罐应用于任何变量的声明,以指定其值不会改变

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.

如何阅读?

只需从右到左阅读每一条语句即可顺利完成

3件主要事情

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。常量int*

在3个地方安排3件事的方法3=6.

i.*开始时

*const int p      [Error]
*int const p      [Error]

二。开始时常量

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。常量常量int*

在4个地方安排4件事情的方法,其中2件是相同的4件/2!=12

i.*开始时

* int const const p     [Error]
* const int const p     [Error]
* const const int p     [Error]
 

二。开始时为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 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]

挤成一体

类型a.p是常量int(5)的指针

const int *p
int const *p

int const const *p
const const int  *p
const int  const *p

类型b.p是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常量*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*常量p,p2;

similarly 
here p is const ptr to int  (type b.)   
but p2 is just int not even cost int

int常量*常量p,p2;

here p is const ptr to const int  (type c.)
but p2 is just const int. 

完成了