[<<][c][>>][..]
Mon Aug 23 18:36:41 CEST 2010

Constant Recursive Data Structures

How to define constant, cyclic data structures in C?

Same as for functions!

The syntax for variable definitions and declarations is the same, it
is the order that matters.  Only the last occurance is a definition,
the rest are declarations.



//-------------------------------

#include <stdlib.h>

struct node;

struct node {
    const struct node **parents;
    const struct node **children;
};

const struct node a;
const struct node b;
const struct node c;
const struct node d;
const struct node e;

const struct node *a_p[] = {NULL};
const struct node *a_c[] = {&c, NULL};
const struct node a = {a_p, a_c};

const struct node *b_p[] = {NULL};
const struct node *b_c[] = {&c, NULL};
const struct node b = {b_p, b_c};

const struct node *c_p[] = {&a, &b, NULL};
const struct node *c_c[] = {&d, &e, NULL};
const struct node c = {c_p, c_c};

const struct node *d_p[] = {&c, NULL};
const struct node *d_c[] = {NULL};
const struct node d = {d_p, d_c};

const struct node *e_p[] = {&c, NULL};
const struct node *e_c[] = {NULL};
const struct node e = {e_p, e_c};


int main() { return 0; }



[Reply][About]
[<<][c][>>][..]