Code snippets:c89 aggr init

From C

Jump to: navigation, search
#include <stdio.h>

#define INIT_FOO(foo, i, d) \
  {0}, * foo ## _init_ = init_foo(&foo, (i), (d))

#define DECLARE_FOO(foo, i, d) \
  s_foo foo = INIT_FOO(foo, (i), (d))

typedef struct s_foo s_foo;

s_foo * init_foo(s_foo * foo, int i, double d);

struct s_foo {
    int i;
    double d;
  };

int main(void) {
    s_foo foo = INIT_FOO(foo, 42, 3.14159);
    DECLARE_FOO(bar, 43, 4.14159);
    int x;

    printf("foo == { %d, %f }\n", foo.i, foo.d);
    printf("bar == { %d, %f }\n", bar.i, bar.d);
    return 0;
  }

s_foo * init_foo(s_foo * foo, int i, double d) {
    foo->i = i;
    foo->d = d;
    return foo;
  }
Personal tools