• 02-14-2002
Unregistered
Lets pretend that I have two variables: *dur and duration.

Now, duration=18.

I want *dur to point to 1/2 of the value in duration, but not change the value of duration.

So duration would still be 18 and dur would be 9. I want it to point to it so whenever duration changes, so does dur. For example if duration is changed to 10, then dur automatically changes to 5 because it points to duration, but is half.

Is there any way to do this?
• 02-14-2002
Stoned_Coder
lots of ways.....
a function to add to both numbers etc.
• 02-14-2002
Unregistered
but not a simple "pointer".
• 02-14-2002
Sorensen
Does dur need to be a pointer?

You could do something like -

Code:

```#include <iostream> using namespace std; #define dur duration/2 int main() {       int duration = 18;     cout << dur << '\n';     duration = 10;     cout << dur << '\n';     return 0;  }```
• 02-14-2002
OneStiffRod
A pointer holds an address and not a value. Your *dur will just point to the address of the variable duration.

They can't be 2 separate values, when you dereference dur you are changing the value in duration.

Do this:
int* duration = 18;
int dur = (*duration)/2; //This holds half the value of duration

If you want to change the value of duration and have dur reflect the new changes you need a function to affect this change.
• 02-15-2002
algruber
summing it up
Looks to me like this is what the answers add up to:

int duration;
int gimmeHalf(int var) {return var/2};
cout <<gimmeHalf(duration);

ciao, Al;)
• 02-16-2002
doubleanti
hmm... if you meant such that dur was a dynamic pointer [in that it changed address based on duration], then you'd just need to ensure that at every change in duration, you reassigned the address which dur was pointing to...