What I normally do is initialise any pointers to NULL before they're used. So that way you can avoid comparing a non-allocated string:
Code:
char *pcStr = NULL;
Allocate(pcStr, "Hello World");
...
void Allocate(char *pcPtr, char *pcString) {
if(pcPtr) delete[] pcPtr; // Ensure that there's nothing
pcPtr = new char[strlen(pcString) + 1];
strcpy(pcPtr, pcString);
pcPtr[strlen(pcString)] = '\0'; // Ensure null-terminated
}
To compare, use strcmp from string.h:
Code:
char *one = "Hello";
char *two = "World";
if(strcmp(one, two) < 0) {
// one is "less" than two (alphabetically)
}
if(strcmp(one, two) == 0) {
// one is equal to two
}
if(strcmp(one, two) > 0) {
// one is greater than two
}
So in your case, the final comparison would be:
Code:
char *pcStr;
if(pcStr && strlen(pcStr) == 0) {
// No value
}
That && syntax is used for "short-circuiting" a comparison, as in - if the first is false, then the rest of the statements are NOT executed. This allows you to verify the pointer BEFORE trying to use it and crashing your program.