@ VernonDozier: That was a typo and I do realize that it wont compile. I do need to use test instead of A. Pointed noted. That being accepted is there an alternate route to the if then else?
The devil is in the details and you have dots there so it's unclear whether there is some sort of pattern. You have this line in a previous post:
id has nothing to do with A[0].a. id is a variable that i pass to the function assign_struct. if id = 3 then A[0].a might be a different value, and if id=4 then A[0].a is another value and so on..
Replacing "A" with "test",
You say id has nothing to do with test[0].a, but then you assign the value of test[0].a based on the value of id in your if-else statement, so that seems like a contradiction. I'm sure you could set up a complex (or not so complex) single or multi-dimensional array or map that could take out all of the branching, but I don't know if that would make the code any cleaner. I think that was what firstPerson was going for here:
void assign_struct(A test[], int id)
{
int A[] = {12,1,2,3,7,3,8 }; //If you know its values during compile time
///then rest of the code :
A[0].b = new int[A[id].a]; //use id as index with its value coordinated
...
except that he named his integer array A, which further compounds the earlier problem of using A instead of test.
You can do something like this, with a 2-D and 3 3-D arrays (one each for b[], c[], and d[] arrays. 2-D array will assign values to A[].a based on id. I don't have time to make a skeleton for the 3-D arrays, but this will create the storage and will fill in the A[].a values.
void assign_struct(A test[], int id)
{
// assumes test[] has size 5, id ranges from 0 to 3
int aValues[4][5] = {{7,4,5,9,7}, {2,1,6,4,8}, {1,5,3,2,8}, {7,3,1,2,8}};
for (int i = 0; i < 5; i++)
{
test[i].a = aValues[id][i];
test[i].b = new int[test[i].a];
test[i].c = new int[test[i].a];
test[i].d = new int[test[i].a];
}
// you can fill in the actual b, c, and d values with 3 separate 3-dimensional arrays.
}