Sudo Reboot
Sudo Reboot

Reputation: 230

Can anyone explain this structure in C?

I have this code

#define MAX_DIM 3
struct kd_node_t
{
    double x[MAX_DIM];
    struct kd_node_t *left, *right;
};

struct kd_node_t wp[] = {
    {{2, 3}}, {{5, 4}}, {{9, 6}}, {{4, 7}}, {{8, 1}}, {{7, 2}}
};

I dont understand the structure declaration in this case.Please help me out

Upvotes: 1

Views: 114

Answers (1)

Antimony
Antimony

Reputation: 39451

This is using brace initialiation.

The array gives a comma separated list initializing each struct in the array (if an explicit size were given, remaining structs would be zero initialized, I believe).

Since they're all similar, just take the first one, {{2,3}}.

This has a single element, {2,3} which tells you how to initialize the x member. Since only two values are specified, the remainder will be zero initialized, giving [2,3,0]. Likewise, left and right are intialized to null pointers.

The rest of the structs in the array are initialized similarly.

Upvotes: 1

Related Questions