Hi,

How do I check if a member of a struct is null?
I've tried:

if(ErrorInfo->TestName != NULL)

The above method returns true even when ErrorInfo->TestName contains nothing (debug shows it as 0xccccccc, "").

(!(strcspn(ErrorInfo->TestName, "") == 0))

The above method throws an access violation.
I've tried other ways too (strcmp etc but they are all throwing access violation).
ErrorInfo->TestName is of type char *.

The structure:

typedef struct 
{
    int ErrorCode;          // the error code which will correspond to an error message
    char *TestName;         // the name of the current test
    char *Command;          // the command that caused the error
    char ErrorText[0x50];   // an error message
    char *ExpectedData;     // the expected data
    char *ReturnedData;     // the returned data
} ErrorInformation;

Note: I can only use ANSI C library functions.
Any help appreciated.

>> How do I check if a member of a struct is null?

Just like you are doing, i.e.

// Is it a NULL pointer?
if(ErrorInfo->TestName != NULL)
{
  // No it isn't ...
}

>> debug shows it as 0xccccccc, "")

This value 0xccccccc tells you that you are dealing with an automatic ErrorInfo variable, whose TestName member has NOT been initialized by you - it's an uninitialized variable. The value you see, is set by the Microsoft's debugging runtime library - a handy feature for spotting uninitialized variables.

So, what you need to do, is to initialize the variables. Either write a constructor which initializes the pointers to NULL, or if you are actually writing C instead of C++ ..

struct ErrorInfo errInfo;
errInfo.TestName = NULL;
// Initialize the rest of the members ..

P.S. An overview regarding various fill patterns and such Win32 Debug CRT Heap Internals.

Thank you, this works :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.