I am declaring one variable based on the condition:
if(abc == 2)
{
price* x;
x = (price *)malloc(2 * sizeof(price*));
}
else if(abc == 3)
{
store* x;
x = (store *)malloc(2 * sizeof(store*));
}
// x is getting used in other function
xyz(&x);
while compile, it is throwing error: error: 'x' was not declared in this scope. I understand that, since x is not defined in the function scope, it is throwing error.
I tried to declare void* x, but that also didn't work. Is there any way we can achieve this?
void *work? It will work if you do it correctly. What problems you face when usingvoid *?void *is an incomplete type. Passxyz(x)instead of indexing it. I am wondering whatxyz()does withxwithout knowing the type ofx.xyzneeds to cast it back toprice*orstore*before it can index it. And it needs some way to know which one it points to.