I have the following struct
struct A
{
int a,
*b,
*c,
*d;
};
I would be creating an array of A and then populating the member variable for each of struct in the array. The members b,c,d are allocated dynamically based on the value of A.a.
To assign the values of A, i have the following function
void assign_struct(A test[], int id)
{
if (id == 1)
{
A[0].a = 2;
A[0].b = new int [A[0].a];
A[0].c = new int [A[0].a];
A[0].d = new int [A[0].a];
A[0].b[0] = 1;
A[0].b[1] = 5;
A[0].c[0] = 10;
A[0].c[1] = 15;
A[0].d[0] = 20;
A[0].d[1] = 25;
A[1].a = 1;
A[1].b = new int [A[1].a];
A[1].c = new int [A[1].a];
A[1].d = new int [A[1].a];
A[1].b[0] = -2;
A[1].c[0] = -56;
A[1].d[0] = 34;
}
if (id == 2)
{
A[0].a = 1;
A[0].b = new int [A[0].a];
A[0].c = new int [A[0].a];
A[0].d = new int [A[0].a];
A[0].b[0] = 18;
A[0].c[0] = 9;
A[0].d[0] = 36;
...
...
...
}
...
...
...
}
I would like to know if there is a way to populate the members of the struct based on 'id' without using and if-then-else or switch-case statement?