Just for completeness, here is the entire buildable test program:
//*************************************************************************
// Copyright (c) 2014 Daniel D Miller
// This utility is freeware, usable for any purpose,
// commercial or otherwise.
//
// static initialization of union within struct
// build with: g++ -Wall -O2 -s union.struct.cpp -o union.struct.exe
//*************************************************************************
#include <stdio.h>
typedef unsigned int uint ;
typedef enum arg_type_e {
ARG_END=0,
ARG_INT,
ARG_UNSIGNED,
ARG_BOOL,
ARG_DOUBLE,
ARG_STRING
} arg_type_t ;
typedef union types_u {
int *i;
uint *u;
bool *b;
double *d;
char *s;
} types_t ;
typedef struct {
char *name;
arg_type_t arg_type;
types_t t ;
} config_entry_t, *config_entry_p;
static uint uint_var = 3254 ;
static double dbl_var = 58.74 ;
#define CHAR_VAR_LEN 80
static char char_var[CHAR_VAR_LEN+1] = "derelict was here" ;
config_entry_t global_table[4] = {
{ (char *) "uvalue", ARG_UNSIGNED, { u: &uint_var }},
{ (char *) "dvalue", ARG_DOUBLE, { d: &dbl_var }},
{ (char *) "svalue", ARG_STRING, { s: char_var }},
{ (char *) "end", ARG_END, { s: NULL }}};
//*******************************************************************
int main(void)
{
uint idx ;
for (idx=0; global_table[idx].arg_type != ARG_END; idx++) {
switch (global_table[idx].arg_type) {
case ARG_INT:
printf("found int: %d\n", *global_table[idx].t.i) ;
break;
case ARG_UNSIGNED:
printf("found uint: %u\n", *global_table[idx].t.u) ;
break;
case ARG_BOOL:
printf("found bool: %s\n", (*global_table[idx].t.b) ? "true" : "false") ;
break;
case ARG_DOUBLE:
printf("found double: %.1f\n", *global_table[idx].t.d) ;
break;
case ARG_STRING:
printf("found string: %s\n", global_table[idx].t.s) ;
break;
case ARG_END:
break;
}
}
return 0;
}