Discuz! Board

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1410|回复: 0
打印 上一主题 下一主题

const char*, char const*, char*const的区别

[复制链接]

388

主题

602

帖子

2218

积分

金牌会员

Rank: 6Rank: 6

积分
2218
跳转到指定楼层
楼主
发表于 2016-6-19 18:40:57 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

事实上这个概念谁都有,只是三种声明方式非常相似很容易记混。
Bjarne在他的The C++ Programming Language里面给出过一个助记的方法:
把一个声明从右向左读。

char * const cp; ( * 读成 pointer to )
cp is a const pointer to char

const char * p;
p is a pointer to const char;

char const * p;
同上因为C++里面没有const*的运算符,所以const只能属于前面的类型。


C++标准规定,const关键字放在类型或变量名之前等价的。

const int n=5;    //same as below
int const m=10;

const int *p;    //same as below  const (int) * p
int const *q;    // (int) const *p

char ** p1;
//    pointer to    pointer to    char
const char **p2;
//    pointer to    pointer to const char
char * const * p3;
//    pointer to const pointer to    char
const char * const * p4;
//    pointer to const pointer to const char
char ** const p5;
// const pointer to    pointer to    char
const char ** const p6;
// const pointer to    pointer to const char
char * const * const p7;
// const pointer to const pointer to    char
const char * const * const p8;
// const pointer to const pointer to const char

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|firemail ( 粤ICP备15085507号-1 )

GMT+8, 2024-5-3 20:45 , Processed in 0.061628 second(s), 19 queries .

Powered by Discuz! X3

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表