I am having problems with a void* to struct. My C++ code is as follows:
struct BUFF_TYPE {
int a;
int b;
};
typedef struct BUFF_TYPE BUFF_TYPE;
struct REG_TYPE {
int c;
void *buff;
};
typedef struct REG_TYPE REG_TYPE;
REG_TYPE regs[20];
const BUFF_TYPE b_A = {3,5};
void func_x() {
int x,y;
regs[4].buff = (BUFF_TYPE*)&b_A;
x = regs[4].buff->a;
y = regs[4].buff->b;
}
It gives me:
error: ‘void*’ is not a pointer-to-object type
in x and y assignments, could you please tell me how to access those a and b values?
BTW, I need the void* as sometimes I need to point to other kinds of buffers.
Before using ->, you must cast regs[4].buff to BUFF_TYPE*, so you need:
((BUFF_TYPE*) regs[4].buff)->a