两者之间有何不同:
char * const
而且
const char *
两者之间有何不同:
char * const
而且
const char *
char * const和const char *有什么区别?
const char * p;
// 值不能被改变
char * const p;
// 地址不能被改变
const char * const p;
// 两者都不能被改变。
这里有一个带有代码的详细解释:
/*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;
}*/
语法:
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 datatype *var
或datatype 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;
}
// 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’
}
我记得在一本关于C语言的捷克书中曾经读到过这样的声明:你需要从变量名开始向左阅读。所以对于
char * const a;
a
是类型为常量指向 char
的指针变量”。char const * a;
a
是指向类型为char的常量变量的指针。希望这有所帮助。”const char * const a;
a
是指向常量字符型变量的常量指针。两个规则
如果const在char和*之间,则会影响左边的那个。
如果const不在char和*之间,则会影响最近的那个。
例如:
char const *。这是指向常量字符的指针。
char * const。这是指向字符的常量指针。
const
修饰符应用于其左侧的术语。唯一的例外是当它左侧没有任何内容时,它就应用于其右侧的内容。
以下都是等效的表达方式,“常量指向常量char
”:
const char * const
const char const *
char const * const
char const const *
我想你是指 const char * 和 char * const。
第一个 const char * 是指向常量字符的指针。指针本身是可变的。
第二个 char * const 是指向字符的常量指针。指针不能改变,它所指向的字符可以改变。
还有 const char * const,其中指针和字符都不能改变。
int const *
(或const int *
)并不是指一个指针指向一个const int
变量,而是这个变量对于这个特定的指针是const
。int var = 10;
int const * _p = &var;
_p
指向一个 const
变量,尽管 var
本身不是常量。