#### Archived

This topic is now archived and is closed to further replies.

# Yet another pointer question :)

## Recommended Posts

Jesper T    322
OK, the situation is like this:
  struct x { double a[2]; }; struct y { double *aptr[2]; }; y.aptr[0] = &x.a[0]; y.aptr[1] = &x.a[1]; // Now, I want to change a[] so that both a[0] and a[1] points to // the same value. So that if you set a[0] = 1.03, then a[1] would // also become 1.03 // Is this even possible ? And it also has to be done through the // *aptr pointer in the y struct 
Can anyone think of a way to do this ? (It''s for a modeler prog im makin, the idea is to fuse two points permanently instead of checking each point if the other one has moved etc.. ) JT

##### Share on other sites
Guest Anonymous Poster
The only way I can think of doing this is to have a[2] be a double pointer. And then have them point to the same address of a double variable.

so

  struct x { double* a[2];}struct y { double *aptr[2];}double temp = 1.03 //or whatever value you wantx.a[0] = &temp;x.a[1] = &temp;y.aptr[0] = x.a[0]; y.aptr[1] = x.a[1]; //These two can both point to a[0] or a[1],//it doesnt mater since they both store the same address.

That should work. Post if it doesnt.

##### Share on other sites
synopsis    122
Hi,

I am not really sure about what you want to do, but anyways:

quote:
Now, I want to change a[] so that both a[0] and a[1] points to the same value.

x.a in your example is an array of doubles, not an array of pointer to double. You cannot have a[1] 'point' at a[0]. Perhaps you are looking for this:

  struct { double* a; double* b; }x; double f1 = 10.0; x.a = &f1; //x.a points to a double x.b = NULL; //x.b points to nirvana... if(x.b) //if x.b points to anything free( x.b ); //free it (replace with delete in C++) x.b = x.a; //Have x.b point to the same data x.a //is pointing to printf("a is: %f. b is: %f",*x.a,*x.b);

Note that x.b does not point to x.a, but at the same memory. Having x.b point at x.a would look similar to:

  struct { double* a; double** b; }x; double f1 = 10.0; x.a = &f1; x.b = NULL; //Now, you want b to point at a x.b = &x.a; printf("a is: %f. b is: %f",*x.a,**x.b);

Hope this helps,

syn

Edited by - synopsis on October 15, 2001 10:57:45 AM

##### Share on other sites
Marius    100
HEre is confusions between pointers to values and values.
Q:
struct x {double a[2];};
struct y {double *aptr[2];};
y.aptr[0] = &x.a[0];y.
aptr[1] = &x.a[1]; Now, I want to change a[] so that both x.a[0] and x.a[1] points to the same value.

MISTAKE IN Q
Here you made a mistake x::a[0]/x::a[1] does not point. They are holding doubles. Besides y::aptr[0] & y::aptr[1] holds pointers to doubles.
you can make the aptr , i(0,1) memebrs to point to the same location. So if
//..sample..
y they;
x thex={1.2, 2.2};
they.aptr[0]=&thex.a[0];
they.aptr[1]=&thex.a[0]; // then both aptr''s point to the same double thex.a[0];

So that if you set a[0] = 1.03, then a[1] would
also become 1.03

// Is this even possible ? And it also has to be done through the // *aptr pointer in the y struct

##### Share on other sites
Jesper T    322
quote:

Now, I want to change a[] so that both a[0] and a[1] points to the same value.

Ok, this was a bit unclear. An array is a pointer, right, would it be more correct to say &a[0] and &a[1] ?

Anyways, I only want want to make sure that a[0] == a[1] at all times, and if I change a[0], then a[1] will also be changed instantly (but not neccasarily a[2] if it exsisted)

Anon poster, you understood my question, but a[] must be an array, not a array of pointers (or else I might aswell rewrite my prog lol) so if doing it like I (tried to) explain above isnt possible, then I''ll just have to do it the hard way

Oh and thanks for the feedback yall, I really appreciate it.

##### Share on other sites
synopsis    122
quote:
An array is a pointer, right ...

No, its not. I think you read something that passing an array to a function is the same as passing a pointer to a function, but that is a different thing.

&a[0], respectively &a[1] give you the addresses of the elements of your array. Say that the array a lies in memory at position
0xa0000000, then &a[0] = 0xa0000000
and
&a[1] = 0xa0000008 (for doubles)

Well, you cannot make sure that 2 variables have the same value at any time. The only thing I could think of is using a class, with an overloaded assignment-operator. That operator would check which element it has been called for, and change the other one if neccessary. Thats not a very good solution, because it will decrease performance, but that is marginal.

btw: rewriting sometimes is the best solution.

##### Share on other sites
Jesper T    322
yeah, I need to either rewrite or justleave out that function.. hmm or maybelimit the amount of points that can be joined so it wouldnt decrease preformance so much. Well, thanks for the help anyways

Xai    1848
The absolute only way to force two variables to stay in sync is to control access to them ...

There is no way in C (or C++) to overide what happens during an assignment to a double ... or an array of doubles ... they are built-in types.

What you want to do is exactly what classes are made for.

So if you want to plug into some already existing code that takes sets double ... but you want to set BOTH doubles ... there is no way at all to do it ...

BUT if you''re writing the client ... you can make it accept a std::pair (or whatever point type you want) ref or pointer ... and then you can derive your class from std::pair (or your point type) ... and code it''s setter functions to always set both.