const char*, char const*, char*const的区别 .
2013-04-13 13:12
267 查看
constchar*,charconst*,char*const的区别问题几乎是C++面试中每次都会有的题目。
事实上这个概念谁都有,只是三种声明方式非常相似很容易记混。
Bjarne在他的TheC++ProgrammingLanguage里面给出过一个助记的方法:
把一个声明从右向左读。
char*constcp;(*读成pointerto)
cpisaconstpointertochar
constchar*p;
pisapointertoconstchar;
charconst*p;
同上因为C++里面没有const*的运算符,所以const只能属于前面的类型。
C++标准规定,const关键字放在类型或变量名之前等价的。
说到这里,我们可以看一道以前Google的笔试题:
[题目]constchar*p="hello";
foo(&p);//函数foo(constchar**pp)
下面说法正确的是[]
A.函数foo()不能改变p指向的字符串内容
B.函数foo()不能使指针p指向malloc生成的地址
C.函数foo()可以使p指向新的字符串常量
D.函数foo()可以把p赋值为NULL.
至于这道题的答案是众说纷纭。针对上面这道题,我们可以用下面的程序测试:
结论如下:
在foo函数中,可以使main函数中p指向的新的字符串常量。
在foo函数中,可以使main函数中的p指向NULL。
在foo函数中,可以使main函数中的p指向由malloc生成的内存块,并可以在main中用free释放,但是会有警告。但是注意,即使在foo中让p指向了由malloc生成的内存块,但是仍旧不能用p[1]='x';这样的语句改变p指向的内容。
在foo中,不能用(*pp)[1]='x';这样的语句改变p的内容。
所以,感觉gcc只是根据const的字面的意思对其作了限制,即对于constchar*p这样的指针,不管后来p实际指向malloc的内存或者常量的内存,均不能用p[1]='x'这样的语句改变其内容。但是很奇怪,在foo里面,对p指向malloc的内存后,可以用snprintf之类的函数修改其内容。
事实上这个概念谁都有,只是三种声明方式非常相似很容易记混。
Bjarne在他的TheC++ProgrammingLanguage里面给出过一个助记的方法:
把一个声明从右向左读。
char*constcp;(*读成pointerto)
cpisaconstpointertochar
constchar*p;
pisapointertoconstchar;
charconst*p;
同上因为C++里面没有const*的运算符,所以const只能属于前面的类型。
C++标准规定,const关键字放在类型或变量名之前等价的。
constintn=5;//sameasbelow intconstm=10; |
constint*p;//sameasbelowconst(int)*p intconst*q;//(int)const*p |
char**p1; //pointertopointertochar constchar**p2; //pointertopointertoconstchar char*const*p3; //pointertoconstpointertochar constchar*const*p4; //pointertoconstpointertoconstchar char**constp5; //constpointertopointertochar constchar**constp6; //constpointertopointertoconstchar char*const*constp7; //constpointertoconstpointertochar constchar*const*constp8; //constpointertoconstpointertoconstchar |
[题目]constchar*p="hello";
foo(&p);//函数foo(constchar**pp)
下面说法正确的是[]
A.函数foo()不能改变p指向的字符串内容
B.函数foo()不能使指针p指向malloc生成的地址
C.函数foo()可以使p指向新的字符串常量
D.函数foo()可以把p赋值为NULL.
至于这道题的答案是众说纷纭。针对上面这道题,我们可以用下面的程序测试:
#include<stdio.h> #include<stdlib.h> #include<stdio.h> voidfoo(constchar**pp) { //*pp=NULL; //*pp="Helloworld!"; *pp=(char*)malloc(10); snprintf(*pp,10,"higoogle!"); //(*pp)[1]='x'; } int main() { constchar*p="hello"; printf("beforefoo%s/n",p); foo(&p); printf("afterfoo%s/n",p); p[1]='x'; return; } |
在foo函数中,可以使main函数中p指向的新的字符串常量。
在foo函数中,可以使main函数中的p指向NULL。
在foo函数中,可以使main函数中的p指向由malloc生成的内存块,并可以在main中用free释放,但是会有警告。但是注意,即使在foo中让p指向了由malloc生成的内存块,但是仍旧不能用p[1]='x';这样的语句改变p指向的内容。
在foo中,不能用(*pp)[1]='x';这样的语句改变p的内容。
所以,感觉gcc只是根据const的字面的意思对其作了限制,即对于constchar*p这样的指针,不管后来p实际指向malloc的内存或者常量的内存,均不能用p[1]='x'这样的语句改变其内容。但是很奇怪,在foo里面,对p指向malloc的内存后,可以用snprintf之类的函数修改其内容。
相关文章推荐
- const char*, char const*, char*const的区别
- const char*, char const*, char*const的区别
- const char* 与 char* const 区别
- 关于const char*, char const* and char *const 等的区别
- const char*, char const*, char*const的区别
- char *const p;与 const char * p;的区别
- const char*, char const*, char*const的区别
- char * const p与 char const * p 区别 一道中兴笔试题
- char *const p;与 const char * p;的区别
- const用法:const *char;char *const;const char *const 区别
- const char *p,char const *p,char *const p 的区别
- const char* p , char const* p 以及 char * const p的区别?
- const char*, char const*, char*const的区别
- const char*, char const*, char*const的区别
- char * const p char const * p const char *p 三个有什么区别?
- C++形参中const char * 与 char * 的区别
- typedef char * pStr的问题和const char*, char const*, char*const的区别
- const char *p/char const *p/char * const p有何区别
- const char *p" 和 "char * const p" 有何区别
- const char*, char const*, char*const的区别