Home > database >  Is there a way to use the #define directive to create a constant struct?
Is there a way to use the #define directive to create a constant struct?

Time:05-14

Let's say I have this struct

typedef struct 
{
    int AM;
    char* name, surname;
}Item;

and I want to define a constant NULLitem with AM = -1 and NULL name/surname. Is there a way to do it with #define?

CodePudding user response:

#define NULLitem (const Item){ .AM = -1, .name = NULL, .surname = NULL }

That's a C99 compound literal.

  • Related