Sign in to follow this  
((__int64)~((__int64)0))

Char Question

Recommended Posts

[quote name='ApochPiQ' timestamp='1317582050' post='4868336']
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 [i]pointer itself[/i] 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 [i]cannot[/i] 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.
[/quote]

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

Share this post


Link to post
Share on other sites
[quote name='((__int64)~((__int64)0))' timestamp='1317591305' post='4868393']
Very good explanation. By far, you're the best programmer I've seen.
[/quote]
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this