I have two structures, with values that should compute a pondered average, like this simplified version:
typedef struct
{
int v_move, v_read, v_suck, v_flush, v_nop, v_call;
} values;
typedef struct
{
int qtt_move, qtt_read, qtt_suck, qtd_flush, qtd_nop, qtt_call;
} quantities;
And then I use them to calculate:
average = v_move*qtt_move + v_read*qtt_read + v_suck*qtt_suck + v_flush*qtd_flush + v_nop*qtd_nop + v_call*qtt_call;
Every now and them I need to include another variable. Now, for instance, I need to include v_clean
and qtt_clean
. I can't change the structures to arrays:
typedef struct
{
int v[6];
} values;
typedef struct
{
int qtt[6];
} quantities;
That would simplify a lot my work, but they are part of an API that need the variable names to be clear.
So, I'm looking for a way to access the members of that structures, maybe using sizeof()
, so I can treat them as an array, but still keep the API unchangeable. It is guaranteed that all values are int
, but I can't guarantee the size of an int
.
Writing the question came to my mind... Can a union
do the job? Is there another clever way to automatize the task of adding another member?
Thanks, Beco
#define
. I think my question is easier because they are all guaranteed to beint
. But nonetheless it is a tip one should keep in mind. – Unemploymentconst int *qt = &(quantities.qtt_move);
? When compiling the line:qt[i]=3;
it returns an error:assignment of read-only location ‘*(qt + (long unsigned int)((long unsigned int)i * 4ul))
. It is not that I'm changing the pointer itself, but the dereference of some index! – Unemploymentconst pointer
) can be found here. – Unemployment