有什么区别:
char * const
and
const char *
有什么区别:
char * const
and
const char *
当前回答
// Some more complex constant variable/pointer declaration.
// Observing cases when we get error and warning would help
// understanding it better.
int main(void)
{
char ca1[10]= "aaaa"; // char array 1
char ca2[10]= "bbbb"; // char array 2
char *pca1= ca1;
char *pca2= ca2;
char const *ccs= pca1;
char * const csc= pca2;
ccs[1]='m'; // Bad - error: assignment of read-only location ‘*(ccs + 1u)’
ccs= csc; // Good
csc[1]='n'; // Good
csc= ccs; // Bad - error: assignment of read-only variable ‘csc’
char const **ccss= &ccs; // Good
char const **ccss1= &csc; // Bad - warning: initialization from incompatible pointer type
char * const *cscs= &csc; // Good
char * const *cscs1= &ccs; // Bad - warning: initialization from incompatible pointer type
char ** const cssc= &pca1; // Good
char ** const cssc1= &ccs; // Bad - warning: initialization from incompatible pointer type
char ** const cssc2= &csc; // Bad - warning: initialization discards ‘const’
// qualifier from pointer target type
*ccss[1]= 'x'; // Bad - error: assignment of read-only location ‘**(ccss + 8u)’
*ccss= ccs; // Good
*ccss= csc; // Good
ccss= ccss1; // Good
ccss= cscs; // Bad - warning: assignment from incompatible pointer type
*cscs[1]= 'y'; // Good
*cscs= ccs; // Bad - error: assignment of read-only location ‘*cscs’
*cscs= csc; // Bad - error: assignment of read-only location ‘*cscs’
cscs= cscs1; // Good
cscs= cssc; // Good
*cssc[1]= 'z'; // Good
*cssc= ccs; // Bad - warning: assignment discards ‘const’
// qualifier from pointer target type
*cssc= csc; // Good
*cssc= pca2; // Good
cssc= ccss; // Bad - error: assignment of read-only variable ‘cssc’
cssc= cscs; // Bad - error: assignment of read-only variable ‘cssc’
cssc= cssc1; // Bad - error: assignment of read-only variable ‘cssc’
}
其他回答
const总是修改在它之前的东西(在它的左边),除非它是类型声明中的第一个东西,在那里它修改在它之后的东西(在它的右边)。
所以这两个是一样的
int const *i1;
const int *i2;
它们定义指向const int类型的指针。你可以改变i1和i2的点,但你不能改变它们的值。
这样的:
int *const i3 = (int*) 0x12345678;
定义一个指向整数的const指针,并将其初始化为指向内存位置12345678。您可以更改地址12345678处的int值,但不能更改i3所指向的地址。
这里是一个详细的解释与代码
/*const char * p;
char * const p;
const char * const p;*/ // these are the three conditions,
// const char *p;const char * const p; pointer value cannot be changed
// char * const p; pointer address cannot be changed
// const char * const p; both cannot be changed.
#include<stdio.h>
/*int main()
{
const char * p; // value cannot be changed
char z;
//*p = 'c'; // this will not work
p = &z;
printf(" %c\n",*p);
return 0;
}*/
/*int main()
{
char * const p; // address cannot be changed
char z;
*p = 'c';
//p = &z; // this will not work
printf(" %c\n",*p);
return 0;
}*/
/*int main()
{
const char * const p; // both address and value cannot be changed
char z;
*p = 'c'; // this will not work
p = &z; // this will not work
printf(" %c\n",*p);
return 0;
}*/
const修饰符应用于紧挨着它左边的项。唯一的例外是,当它的左边没有任何东西时,它就适用于它右边的东西。
这些都是“指向常量char的常量指针”的等效方式:
Const char * Const Const char Const * Char const * const Char const const *
另一个经验法则是检查const的位置:
* =>之前存储的值为常量 * =>指针本身是常量
两个规则
如果const在char和*之间,它将影响左边的那个。 如果const不在char和*之间,它将影响最近的一个。
e.g.
Char const *。这是一个指向常量char的指针。 Char * const。这是一个指向char类型的常量指针。