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

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


当前回答

如果const在*的左边,它指的是值(不管它是const int还是int const)如果const位于*的右侧,则它引用指针本身可以同时使用

重要的一点是:constint*p并不意味着你所指的值是常量!!。这意味着您不能通过该指针更改它(意味着您不能分配$*p=…`)。值本身可能会以其他方式更改。如

int x = 5;
const int *p = &x;
x = 6; //legal
printf("%d", *p) // prints 6
*p = 7; //error 

这主要用于函数签名,以确保函数不会意外更改传递的参数。

其他回答

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

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

常量参考:对变量(此处为int)的引用,该变量是常量。我们主要将变量作为引用传递,因为引用的大小比实际值小,但有一个副作用,这是因为它像实际变量的别名。通过对别名的完全访问,我们可能会意外地更改主变量,因此我们将其设置为常量以防止这种副作用。int var0=0;常量int&ptr1=var0;ptr1=8;//错误var0=6;//好啊常量指针一旦常量指针指向一个变量,那么它就不能指向任何其他变量。int var1=1;int var2=0;int*const ptr2=&var1;ptr2=&var2;//错误指向常量的指针不能改变所指向变量值的指针称为常量指针。int const*ptr3=&var2;*ptr3=4;//错误指向常量的常量指针指向常量的常量指针是一个既不能改变它所指向的地址,也不能改变保存在该地址的值的指针。int var3=0;int var4=0;const int*const ptr4=&var3;*ptr4=1;//错误ptr4=&var4;//错误

为了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);

简单助记符:

类型指针<-*->指针对象名称


我喜欢把int*I看作是声明“I的解引用是int”;在这个意义上,constint*i表示“i的deref是constint”,而int*consti表示“consti的derref是int”。

(像这样思考的一个危险是,它可能会导致人们倾向于int const*i风格的声明,而人们可能会讨厌/拒绝这种声明)

常量的简单使用。

最简单的用法是声明一个命名常量。为此,我们声明一个常量,就像它是一个变量一样,但在它之前添加const。我们必须立即在构造函数中初始化它,因为当然,以后不能设置值,因为这会改变它。例如:

const int Constant1=96; 

将创建一个值为96的整数常量,无想象力地称为常量1。

这些常数对于程序中使用但在程序编译后不需要更改的参数是有用的。与C预处理器#define命令相比,它对程序员有一个优势,即编译器本身可以理解和使用它,而不仅仅是在到达主编译器之前由预处理器替换到程序文本中,因此错误消息更有用。

它也适用于指针,但必须注意const的位置,以确定指针或指针指向的对象是常量还是两者都是常量。例如:

const int * Constant2 

声明Constant2是指向常量整数的变量指针,并且:

int const * Constant2

是一种替代语法,其作用相同,而

int * const Constant3

声明Constant3是指向变量整数的常量指针

int const * const Constant4

声明Constant4是指向常量整数的常量指针。基本上,“const”适用于其紧邻左侧的任何内容(除非那里什么都没有,在这种情况下,它适用于紧邻右侧的任何内容)。

裁判:http://duramecho.com/ComputerInformation/WhyHowCppConst.html