Kendall Hopkins
Kendall Hopkins

Reputation: 44154

How do I initialize a struct in C based on the member's name

In C I often see structs init'ed like this:

struct size aSize;
aSize.x = 100;
aSize.y = 42;

But in other languages you can create struct "like" data structures in one line like:

aSize = {
    x : 100,
    y : 42
};

Is a similar syntax supported in C?

I understand that Javascript's "struct" like data structure is really a hash without defined params, i'm just trying to show the syntax

Upvotes: 2

Views: 1174

Answers (1)

SiegeX
SiegeX

Reputation: 140567

C99 allows the following for "order agnostic" initilization:

struct aSize {
    int x;
    int y;
} aSize = { .y = 4, .x = 5 };

See this link for a working example

Upvotes: 6

Related Questions