0

I am working on C Structures. I want to #define the struct values as follows.

// #define get_x() { .y = { .z = 1, .c = "test" } };   // this is working

// But I want to replace the above line with 
#define get_y() { .z = 1, .c = "test" };
#define get_x() { .y = get_y() };                     // this gives error

struct :
typedef struct { 
    int z;
    char c[10]; 
} y_t;

typedef struct { 
    y_t y;
} x_t;

int main()
{
    x_t x = get_x();
    printf("c: %s", x.y.c);
    return 0;
}

Will you please help me know, How can I do that?

shiv patil
  • 99
  • 1
  • 1
  • 6
  • Have you thought about the almost invisible `;` at the end of the `get_y()` that is giving you grief? There's no `;` between two curly braces in the "working version"... – Fe2O3 Aug 09 '22 at 08:40

1 Answers1

2

Remove the ;, there is no ; in initialization. Typically, macros are in upper case.

#define GET_Y() { .z = 1, .c = "test" }
#define GET_X() { .y = GET_Y() }
KamilCuk
  • 120,984
  • 8
  • 59
  • 111