I have a function that returns an array of different lengths based on a table lookup. I am malloc'ing required memory for it inside the function but then how can I fill the array from its pointer? The compiler is throwing same error for both of my tries (commented lines). Please help!
int lookup(const char *name, float *factors) {
int length;
if(!strcmp(name, "foo")) {
length = 6;
factors = malloc(length * sizeof(float));
// *factors = {0, -0.9, -4.9, -8, -7.8, -23.9};
// factors = {0, -0.9, -4.9, -8, -7.8, -23.9};
}
else if(!strcmp(name, "bar"))
{
length = 4;
factors = malloc(length * sizeof(float));
// *factors = {0, -3, -6, -9};
}
// .......................
// more else if branches
// .......................
else // error: name not found in table
{
factors = NULL;
fprintf(stderr, "name not found in table!!\n");
return 0;
}
return length;
}