关于const char*, char const* and char *const 等的区别

 

 

注意: const 和 typedef 一起使用的时候比较特殊

陷阱一:

记住,typedef是定义了一种类型的新别名,不同于宏,它不是简单的字符串替换。比如:
先定义:
typedef char* PSTR;
然后:
int mystrcmp(const PSTR, const PSTR);

const PSTR实际上相当于const char*吗?不是的,它实际上相当于char* const。
原因在于const给予了整个指针本身以常量性,也就是形成了常量指针char* const。
简单来说,记住当const和typedef一起出现时,typedef不会是简单的字符串替换就行。

陷阱二:

typedef在语法上是一个存储类的关键字(如auto、extern、mutable、static、register等一样),虽然它并不真正影响对象的存储特性,如:
typedef static int INT2; //不可行
编译将失败,会提示“指定了一个以上的存储类”。

 

1 关于const char*, char const* and char *const 等的区别
2
3 助记方法:
4 把一个声明从右向左读
5 比如:
6 char * const cp;
7 //cp is a const pointer to char
8
9 const char * cp;
10 //cp is a pointer to const char;
11 规则:
12 //从右到左读:
13 // * 读成 pointer to
14 // ** 读成 (a) pointer to (a) pointer to
15 // * const 读成 const pointer to
16 再比如:
17 char ** cpp; //cpp is a pointer to ( a pointer to char )
18
19 const char ** cpp; // cpp is a pointer to ( a pointer to const char )
20
21 char * const * cpp; //cpp is a pointer to const pointer to char
22
23 const char * const * cpp; //cpp is pointer to const pointer to const char
24
25 char ** const cpp; //cpp is const pointer to pointer to char
26
27 char * const * const cpp;//cpp is const pointer to const pointer to char
28
29 const char * const * const cpp; //cpp is const pointer to const pointer to c
onst char
30
31
32 //×××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××
33 //关键:
34 //只要记住:
35 //(1)把声明从右到左读
36 //(2)* 读成 pointer to
37 //×××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××

我们都用过const 修饰符修饰一个变量,而且我们知道使用const修饰之后,这个变量相当于常量了,他的值在代码其他部分不能再修改,这对于函数调用时候,防止错误的修改原本不应修改的变量起到很大作用,举个例子,加入A函数调用B函数进行字符串处理,A把字符串的地址传递给BB在处理过程中误修改了字符串的地址,导致程序出错,有时候这种错误很难找到,这时候我们就可以使用const关键字。

              可是对于指针的情况,我们不想修改指针指向的地址,但是允许修改指针指指向的地址中的值,该怎么做呢,可以这么理解这段话,如果 char * a = ptr,那么a不能在指向其他的地址,但是对于*a的赋值是可以的。那么如果我们写const char *或者char const *或者

char * const,哪一个是我们需要的呢?看一段代码。

1 #include <stdio.h>

2 int main(void)

3 {

4     char buf[] = "hello world";

5     char buf2[] = "world hello";

6     const char* a = buf;

7     char const* b = buf;

8     char* const c = buf;

9     //*a = 'x';

10     //*b = 't';

11     *c = 't';

12     a = buf2;

13     b = buf2;

14     //c = buf2;

15     printf("a is %s/nb is %s/nc is %s /n", a, b, c);

16     return 0;

17 }

如果我把第8行第9行的注释去掉,编译的时候会发生如下错误:(编译环境gcc

test.c: In function `main':

test.c:9: error: assignment of read-only location

test.c:10: error: assignment of read-only location

如果14行注释去掉会发生如下错误:

test.c: In function `main':

test.c:14: error: assignment of read-only variable `c'

好了我么总结一下,const char *char const* 效果一样,都是不允许修改指针指向的地址空间的值,即把值作为常量,而char * const则是不允许修改指针自身,不能再指向其他地方,把指针自己当作常量使用。需要注意的是,使用char * const 定一个常量指针的时候一定记得赋初始值,否则再其他地方就没法赋值了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值