Member Avatar for nalasimbha

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?

First tell me how id has something to do with A[0].a

I mean you have

if (id == 1)	
 { A[0].a = 2; ... }
else if(id == 2) { A[0].a = 1; ... }

How does id correlates to A[0].a?
What would A[0].a be if id is 3,4,5,6,7,8,9,10 ?

Member Avatar for nalasimbha

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..

You should replace A with test everywhere inside your function body. A is a type. test is the variable itself. test is an array, not A . You cannot dereference A with the [] operator because there is nothing to dereference.

void assign_struct(A test[], int id)
{
   // replace "A" with "test" inside here.
}

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..

Then just do this :

void assign_struct(A test[], int id)
{
   switch(id)
   {
      case 0 : A[0].a = 12;
      case 1 : A[0].a = 2;
      case 2 : A[0].a = 1
      //so on
    }

  ///then rest of the code :
    A[0].b = new int[A[0].a];
    ...

or if you know the values before compile time

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
    ...
Member Avatar for nalasimbha

Thank you firstPerson. But the question remains to be answered.Is there no way to do the assignments without the if then else or switch statements. Your solutions have switch statement in it.

Thank you firstPerson. But the question remains to be answered.Is there no way to do the assignments without the if then else or switch statements. Your solutions have switch statement in it.

I'll reiterate what I said in my last post. The code is not going to compile with terms like A[0].b in it and it has nothing to do with whether you use if-statements or switch statements. A is a struct definition, not a variable. You cannot dereference it with the [] operator. Replace "A" with "test".

Member Avatar for nalasimbha

@ 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?

@ 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.
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.