cerr cerr - 1 year ago 60
C Question

How to keep a global storage area type independent in C?

I have to secure space in advance sufficient for up to 4k total entries divided between two different types, in any proportion, then fill it with data. I thought I'd allocate space sufficient for 4k of the larger type, and then decide upon filling it up with data what kind it actually is. I'm running into an issue where the compiler tells me error (etoa:1554): illegal type(s): ptr-to-void '+' int.

In my init function, I allocate the space something like this:

ser_dat_ch = calloc(LABEL_T_CNT,
(sizeof(mpls_vpws_t) > sizeof(mpls_vpls_t)
? sizeof(mpls_vpws_t)
: sizeof(mpls_vpls_t)));

is declared at file scope like so:

static void *ser_dat_ch = NULL;

When I load data into the structure, I wanted to do something like

rv = switch_mpls_vpws_data_get(lab, (mpls_vpws_t*)&ser_dat_ch[lab]);

, but this is what gets the compiler barking at me. What's the problem?

Answer Source

In this particular case, I think the simplest solution would be to create a buffer with 4K entries of an union type.

struct A {/**/};
struct B {/**/};

union AB {
    struct A a;
    struct B b;

union AB ser_dat_ch[4000];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download