Thread: Advanced question on pointers

  1. #1
    Unregistered
    Guest

    Advanced question on pointers

    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?

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    lots of ways.....
    a function to add to both numbers etc.
    operator overloading if you wrap it all up in a class
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Unregistered
    Guest
    but not a simple "pointer".

  4. #4
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    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;   
    }

  5. #5
    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.
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    20

    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

  7. #7
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    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...
    hasafraggin shizigishin oppashigger...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question about pointers in functions
    By occams razor in forum C Programming
    Replies: 3
    Last Post: 05-15-2007, 12:16 AM
  2. Newbie question about pointers in function parameters.
    By sojurn in forum C++ Programming
    Replies: 14
    Last Post: 01-20-2007, 09:21 PM
  3. Newbie question: pointers, other program's memory
    By xxxxme in forum C++ Programming
    Replies: 23
    Last Post: 11-25-2006, 01:00 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM