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

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


当前回答

两边带有int的常量将使指针指向常量int:

const int *ptr=&i;

or:

int const *ptr=&i;

*后的const将使常量指针指向int:

int *const ptr=&i;

在这种情况下,所有这些都是指向常量整数的指针,但没有一个是常量指针:

 const int *ptr1=&i, *ptr2=&j;

在这种情况下,所有指针都指向常量整数,ptr2是指向常量整数的常量指针。但ptr1不是常量指针:

int const *ptr1=&i, *const ptr2=&j;

其他回答

如果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 

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

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

我想这里已经回答了所有问题,但我只想补充一点,你应该小心typedefs!它们不仅仅是文本替换。

例如:

typedef char *ASTRING;
const ASTRING astring;

跨接的类型是char*const,而不是const char*。这是我总是倾向于将常量放在类型的右边,而从不放在开头的原因之一。

最初的设计者多次将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指针。

要简单地记住:

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

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

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

e.g.

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