首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

char *const, char const* const char

2012-11-26 
char *const, char const*, const char*5.4.1 Pointers and Constants [ptr.pc]When using a pointer, two

char *const, char const*, const char*

5.4.1 Pointers and Constants [ptr.pc]

When using a pointer, two objects are involved: thepointer itself and the object pointed to. ‘‘Pre-fixing’’ a declaration of apointer withconst makes the object, but not the pointer, a constant. To declarea pointer itself, rather than the object pointed to, to be a constant, we usethe declarator operator*const instead of plain*.Forexample:

 

void f4(){       int a = 1;       const intc = 2;       const int*p1 = &c;      // ok       const int*p2 = &a;      // ok       int* p3 =&c;        // error: initialization ofint* with const int*       *p3 = 7;               // try to change the value of c}

It is possible to explicitly remove the restrictionson a pointer to const by explicit type conversion (§10.2.7.1and §15.4.2.1)


Reference:

The.C++.Programming.Language.3rd.Edition

http://www.cnblogs.com/Flouse/archive/2008/08/14/constPointer.html

热点排行