Const char* vs char* const

Programming/C & C++ 2014. 12. 24. 21:53
반응형
const int a = 1; // read as "a is an integer which is constant"
int const a = 1; // read as "a is a constant integer"

Both are the same thing. Therefore:

a = 2; // Can't do because a is constant

The reading backwards trick especially comes in handy when you're dealing with more complex declarations such as:

const char *s;      // read as "s is a pointer to a char that is constant"
char c;
char *const t = &c; // read as "t is a constant pointer to a char"

*s = 'A'; // Can't do because the char is constant
s++;      // Can do because the pointer isn't constant
*t = 'A'; // Can do because the char isn't constant
t++;      // Can't do because the pointer is constant

출처 : http://stackoverflow.com/questions/162480/const-int-vs-int-const-as-function-parameter-in-c-and-c

반응형

댓글을 달아 주세요