I do not understand how you could have a pointer to a string literal. and the condition to,
1 2
constchar *cp = "Hello World"; // is this possible, cuz it compiles
if (cp && *cp) // i think the first would be true if cp points to something and the second if it has a value other than 0
Exercise: Explain the behavior of the condition in the following if
constchar *cp = "Hello World"; // is this possible, cuz it compiles
Yes that is perfectly acceptable.
What it's basically doing is it's putting that "Hello World" string somewhere in memory, and giving you a pointer to it. More specifically... the pointer will actually point to the first character in the string.
if (cp && *cp) // i think the first would be true if cp points to something and the second if it has a value other than 0
The first will be true if cp is a non-null pointer. Which in this case will be true because cp points to the "Hello World" data.
The second will be true if the character cp points to is nonzero. In this case it is true because the character cp points to is 'H', which is nonzero.
Any pointer that is not null. A pointer is a variable that holds a value that is essentially an unsigned integer and the value is assumed to be a memory address. Null pointer contains value 0, which is not considered to be a valid address.
(One can naturally store a random number, like 7, in a pointer. It is not null, but in practice it is not a valid address either.)