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?

1
  • 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"... Commented Aug 9, 2022 at 8:40

1 Answer 1

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() }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.