Pointer to structure

Hi all,

There is a structure:

1
2
3
4
5
6
7
8
9
10
11
12
13
struct struct_cent_3D
{	
        ...
	struct sGrad 
	{
		vectorize u;
		vectorize v;
		vectorize w;
		vectorize T;		
		vectorize *x;
	} grad;
        ...
} cent[size];


I want to make "*x" to point "u" for all "cent". That is, for all members of array,

cent[].grad.x = & cent[].grad.u

Can I just do this for only the first member,

cent[0].grad.x = & cent[0].grad.u

I don't want to make that for the whole array members one by one, that is for i=0,1,2,...,size

Regards.

Last edited on
I don't want to make that for the whole array members one by one, that is for i=0,1,2,...,size


That's what you have to do. Just put it in a loop:

1
2
for(int i = 0; i < size; ++i)
  cent[i].grad.x = &cent[i].grad.u;



Only way around it would be to do this in a constructor:

1
2
3
4
5
6
7
8
9
10
11
12
13
struct sGrad 
{
  sGrad()  // constructor -- called for each object when it is created
  {
    x = &u;  // set this pointer in the constructor
  }

  vectorize u;
  vectorize v;
  vectorize w;
  vectorize T;		
  vectorize *x;
} grad;
Last edited on
Disch, thank you for your help with instant messages.
Topic archived. No new replies allowed.