Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all,in an effecitve c++ book,i have seen that the constants has been declared as " const char* const authoeName = "Scott Meyers". can anybody please tell me the meaning of this statement?why we should use two 'const' keyword?please help me....
thank you in advance...
Posted

Here const is coming before the * and after the *. so, neither the address nor the value pointed authoeName can be changed.Means,
const char* const authoeName = "Scott Meyers"; after this the following statements will give compilation errors,

*authoeName = "New author";
authoeName  = &NewChar; 


Also you should initialize authoeName in this case.otherwise compiler will give error.

Also check this link for more info
http://www.possibility.com/Cpp/const.html[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 13-Jun-11 2:38am    
Correct, my 5. More exactly, there are three cases of using const in string.
Please see my solution.
--SA
Jijesh Balakrishnan 13-Jun-11 4:42am    
thank you
A constant, some object which you cannot modify. This is a very important feature of practically all languages. The technical representation of constants of constants is application-dependent. Parameters of functions can be constant as well.

When it comes to pointers, you need to know the difference between the following cases:

C#
const char* constantString = "My constant string";
constantString++; //can do this
// cannot change the content of the string,
// next line won't compile:
//contstantString[0] = 'a';

char* const constantPointer = "My constant pointer";
constantPointer[0] = '!'; //can do this
// cannot modify pointer,
// next line won't compile:
//constantPointer++;

//cannot modify content or pointer:
const char* const readOnly = "Don't touch!";


If there something else you need to get explained?

—SA
 
Share this answer
 
Comments
Jijesh Balakrishnan 13-Jun-11 4:42am    
thank you
Sergey Alexandrovich Kryukov 13-Jun-11 4:43am    
You're very welcome.
Thanks for accepting the answer.
Good luck, call again.
--SA
to understand this you need to understand the difference between
1. pointer to constant
2. constant pointer.

1. pointer to constant
int n1 = 10;
int* pn = &n1;


suppose the address of n1 is 0x1001.
when we say pn, it is equal to 0x1001. when we say, *pn it is equal to 10;
see the below
int n1 = 10;
const int* pn = &n1;

this is a pointer to a constant integer.
means the pointer is not constant. but to what it points to is a constant;

*pn = 10; // will cause an error in this case. because we are trying to change the value of what pn is pointing to.

int n2 = 20;
pn = &n2; /* This will not cause any errors. becuse we are changing the pointer value. pn is pointing to n2 instead of n1.No problem at all*/

2. constant pointer
is the other way round of above

int n1 = 10;
int* const pn = &n1;
*pn = 10; // No error

int n2 = 20;
pn = &n2; // will cause an error

keeping all the above in mind we can say
const char* const authoeName = "Scott Meyers" /* is a constant pointer to a constant character. both the pointer and what it points to is constant.*/

const char* const authoeName = "Scott Meyers";
char* bookname = "Effective c++";

authoeName = bookname; // Error
*authoeName = *bookname; // Error

both kind of alteration will cause an error
 
Share this answer
 
Comments
Jijesh Balakrishnan 13-Jun-11 4:42am    
thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900