# Char Question

This topic is 2933 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello, GameDev members. I have a question about chars. Why is const char* like a string? It's kind of weird, because the const keyword means it is constant, and cannot change, yet it can. If I char* to anything else, I get errored. Including wchar_t.

Thanks you guys

##### Share on other sites
+1 on the std::string
and with a string you can still convert it easily to a c-string if you have to.
IE:

 const char* cstr; string str = "testing string"; cstr = str.c_str(); 

##### Share on other sites

One way to represent a string in memory is a linear sequence of characters, sometimes with a special character to signify the end of the text ("null" or 0 is used for this purpose in many environments, for instance). Another way to think of this is as an array of characters. In C and C++, you can denote an array of unknown size by referring to the address of its first element via a pointer. So an array of characters (char) can be "decayed" into a pointer to a character (char *). By convention, raw strings in C/C++ are therefore often denoted by char*.

The const is a bit trickier. In C/C++, there is a certain order in which you must read type signatures. It's more or less right to left, so const char* would be a "pointer to a char (or array of char) where the pointed-to char(s) is/are constant." This is subtly different from char * const, which is a pointer to a char where the pointer itself cannot change. So if you have several arrays of char (or strings), you can take a single const char* foo and point it to one of those strings, then change it to point to another, and so on.

However, what you cannot do is modify the contents of the string itself. This is important in general, but there's one particular case that's especially so: string literals. If you have const char* foo = "bletch"; in your program, the memory for the string "bletch" is often (not always, but often) allocated by the compiler in a way that is not writable. In fact, on many operating systems, trying to change it might crash the program instantly. Moreover, "bletch" might be right next to "quux" in memory. so if you change the string to "bletcherous", you overwrite the "quux" entirely! This is obviously bad. Therefore, string literals are const in C/C++, and by convention, any string you can't safely muck around with is also const.

Your last question hits on another facet of C/C++ that is important: type equivalence. Just because a char* (or const char*) is a string doesn't mean that you can just start doing string-like things with it; for example, you can't just convert to a wchar_t* because that's a different "type." (In Visual C++ on Windows, wchar_t is typically 16 bits to a char's 8 bits. On Unixes and with non-Microsoft compilers on Windows, it is more often 32 bits.) You have to call a function to do the conversion one character at a time, because the compiler doesn't know anything about strings - it just moves bytes around.

All of this may strike you as weird and slightly inconvenient - and it is! If you happen to be using C++, you have a far better alternative available, via std::string.

Very good explanation. By far, you're the best programmer I've seen.

##### Share on other sites

Very good explanation. By far, you're the best programmer I've seen.

He is certainly a capable programmer. Here, however, ApochPiQ displays something different: the ability to clearly explain technical concepts in a manner understandable to those less knowledgeable. Which is a far rarer skill; and, sadly, one which few who teach possess.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 12
• 30
• 9
• 16
• 12