有什么区别:
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’
}
其他回答
常量指针:在整个程序中,常量指针只能指向相应数据类型的单个变量。我们可以改变指针所指向的变量的值。初始化应该在声明本身的时候进行。
语法:
datatype *const var;
Char *const属于这种情况。
/*program to illustrate the behaviour of constant pointer */
#include<stdio.h>
int main(){
int a=10;
int *const ptr=&a;
*ptr=100;/* we can change the value of object but we cannot point it to another variable.suppose another variable int b=20; and ptr=&b; gives you error*/
printf("%d",*ptr);
return 0;
}
指向const值的指针:在这种情况下,指针可以指向任意数量的相应类型的变量,但不能改变指针在特定时间所指向的对象的值。
语法:
Const *var数据类型
Const char*属于这种情况。
/* program to illustrate the behavior of pointer to a constant*/
#include<stdio.h>
int main(){
int a=10,b=20;
int const *ptr=&a;
printf("%d\n",*ptr);
/* *ptr=100 is not possible i.e we cannot change the value of the object pointed by the pointer*/
ptr=&b;
printf("%d",*ptr);
/*we can point it to another object*/
return 0;
}
两个规则
如果const在char和*之间,它将影响左边的那个。 如果const不在char和*之间,它将影响最近的一个。
e.g.
Char const *。这是一个指向常量char的指针。 Char * const。这是一个指向char类型的常量指针。
const总是修改在它之前的东西(在它的左边),除非它是类型声明中的第一个东西,在那里它修改在它之后的东西(在它的右边)。
所以这两个是一样的
int const *i1;
const int *i2;
它们定义指向const int类型的指针。你可以改变i1和i2的点,但你不能改变它们的值。
这样的:
int *const i3 = (int*) 0x12345678;
定义一个指向整数的const指针,并将其初始化为指向内存位置12345678。您可以更改地址12345678处的int值,但不能更改i3所指向的地址。
区别在于const char *是指向const char类型的指针,而char * const是指向char类型的常量指针。
首先,被指向的值不能改变,但指针可以改变。第二,被指向的值可以改变,但指针不能改变(类似于引用)。
还有一个
const char * const
它是一个指向常量char的常量指针(因此它的任何内容都不能更改)。
注意:
以下两种形式是等价的:
const char *
and
char const *
具体的原因在c++标准中有描述,但重要的是要注意并避免混淆。我知道一些编码标准更喜欢:
char const
over
const char
(带或不带指针),这样const元素的位置与使用指针const时相同。
我想指出,使用int const *(或const int *)不是关于指向const int变量的指针,而是这个变量对于这个特定的指针是const的。
例如:
int var = 10;
int const * _p = &var;
上面的代码可以很好地编译。_p指向一个const变量,尽管var本身不是常量。