By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,657 Members | 994 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,657 IT Pros & Developers. It's quick & easy.

Difference Between Const Char and Char

P: 57
Difference Between Const Char and Char
Jun 28 '06 #1
Share this Question
Share on Google+
4 Replies


Banfa
Expert Mod 5K+
P: 8,916
const char is a constant and the value can not be changed char is a variable and the value can be changed, that is

Expand|Select|Wrap|Line Numbers
  1. const char cc = 'A';
  2. char c = 'B';
  3.  
  4. c = 'C';    /* Legal */
  5.  
  6. cc = 'Z';   /* will cause a compiler error because it is constant */
  7.  
Jun 28 '06 #2

P: 57
Thanks!!!!!!!!
Jun 28 '06 #3

P: 2
In case of const char, the poiinter variable is not fixed, whereas the string
is fixed.

It means that for example, consider the following.

const char *string = "Hello".
*string = 'm'; // Gives error message
string = "Hello my friend" // works

Coming to ordinary char, a char variable can contain a single character only.
In case of character array, it can hold that many number of characters
specified in the dimension.

char status; // can contain one char variable
char myname[10] // can hold ten characters
Jun 29 '06 #4

Banfa
Expert Mod 5K+
P: 8,916
In case of const char, the poiinter variable is not fixed, whereas the string is fixed.
Actually in the case of const char there are no pointers.

What you say usefully applies to const char * but that was not the type mentioned in the question.

It is worth noting that const can become a little confusing when pointer come into the mix

const <TYPE> * - this is the most common constant pointer seen which is a pointer to constant data (that is as stated above you can change the pointer but you cant change the data)

but you can also have

<TYPE> * const - this is a constant pointer to variable data, that is you can change the data that the pointer is pointing to but you can not change the pointer itself

const <TYPE> * const - a constant pointer to constant data, you can not change the pointer and you can not change the data the pointer points to.
Jun 29 '06 #5

Post your reply

Sign in to post your reply or Sign up for a free account.