I believe hwndTitleValue is a pointer, at least in Hungarian notation. Your method allocates an array of bytes (ANSI C string), so the best way to do this is
#include <string.h> // ... other includes ... int isEmpty(LPSTR string) { if (string != NULL) { // Use not on the result below because it returns 0 when the strings are equal, // and we want TRUE (1). return !strcmp(string, ""); } return FALSE; }
You can, however, hack the above method and not use strcmp:
#include <string.h> // ... other includes ... int isEmpty(LPSTR string) { // Using the tip from Maciej Hehl return (string != NULL && string[0] == 0); }
It should be noted that the line may be empty, but filled with a space. This method will tell you that the string contains data (spaces are data!). If you need to account for lines filled with spaces, you need to trim them first.
EDIT . Another thing to note is that the above methods do not take into account valid NULL pointers. If the pointer is null, isEmpty will return FALSE, which is undesirable. We can remove the NULL check, and then it becomes the responsibility of the caller, or we can determine that isEmpty returns FALSE to NULL.
#include <string.h> // ... other includes ... int isEmpty(LPSTR string) { // Always return FALSE to NULL pointers. if (string == NULL) return FALSE; // Use not on the result below because it returns 0 when the strings are equal, // and we want TRUE (1). return !strcmp(string, ""); }
Bruno brant
source share