extern "C" {
typedef struct Pair_s {
char *first;
char *second;
} Pair;
typedef struct PairOfPairs_s {
Pair *first;
Pair *second;
} PairOfPairs;
}
Pair pairs[] = {
{"foo", "bar"}, //this is fine
{"bar", "baz"}
};
PairOfPairs pops[] = {
{{"foo", "bar"}, {"bar", "baz"}}, //How can i create an equivalent of this NEATLY
{&pairs[0], &pairs[1]} //this is not considered neat (imagine trying to read a list of 30 of these)
};
How can I achieve the above style declaration semantics?
In C++11 you could write:
Do note the implications of using the free store: objects allocated there are not destructed at the end of the execution of your program (like static objects are) or anytime else (without a corresponding
delete). Probably not a concern in hosted implementations if Pair is a C struct and does not manage resources (and you always expected your program to use that memory until it exits).edit: If you can’t use C++11 features, you can always create a helper function. Example: