'Is there a way to use the #define directive to create a constant struct?
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?
Solution 1:[1]
#define NULLitem (const Item){ .AM = -1, .name = NULL, .surname = NULL }
That's a C99 compound literal.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Jonathan Leffler |