This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Pointer to NULL structure

I have a structure defined as

typedef struct example{
int a;
char b;
example c;
}STRUCT_EG;

and I declare a pointer to this structure as

STRUCT_EG *Struct_ptr

To check whether the pointer is pointing to NULL I use the following "if" statement

if (Struct_ptr == (STRUCT_EG*)0)

I was wondering whether this method is acceptable as I could not get it to work correctly (ie the "if" condition doesnt go through eventhough the pointer is null). Are there any other ways which I can use?

Please advise. Thank you.

Parents
  • if (Struct_ptr == (STRUCT_EG*)0)

    That should work. The simpler construct
    if (Struct_ptr == NULL)
    should, too, assuming you've #include'ed at least one of the Standard C headers that #define NULL (e.g., stdio.h or stdlib.h).

    Actually, even
    if (Struct_ptr == 0)
    has to work. If it didn't, that'd be a compiler bug.

    [A reminger: the tag to put code samples in is < pre >, not < b >.

Reply
  • if (Struct_ptr == (STRUCT_EG*)0)

    That should work. The simpler construct
    if (Struct_ptr == NULL)
    should, too, assuming you've #include'ed at least one of the Standard C headers that #define NULL (e.g., stdio.h or stdlib.h).

    Actually, even
    if (Struct_ptr == 0)
    has to work. If it didn't, that'd be a compiler bug.

    [A reminger: the tag to put code samples in is < pre >, not < b >.

Children
No data