Thread: Will this work?

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    278

    Will this work?

    First, let me point out that my grasp of pointers is thin at best! Will the following function do what I want?

    Code:
    void Get_Timestamp(time_t *tOld_Timestamp, time_t *tNew_Timestamp) {
      *tOld_Timestamp = *tNew_Timestamp;
      *tNew_Timestamp = time(NULL);
    }
    If I was using ints I would know exactly what to do. After the function is over, I want tOld_Timestamp to point to the value that tNew_Timestamp used to point to, and I want tNew_Timestamp to point to whatever the result of the time() function call is.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, what happens here is that what tOld_Timestamp points to becomes a copy of what tNew_Timestamp points to, while what tNew_Timestamp points to is assigned a new value. It is probably what you intended, but technically the pointers continue to point to what they pointed to before the function was called.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    The pointers still point to the same addresses, but the contents of those addresses has changed how I expect right?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Bladactania
    The pointers still point to the same addresses, but the contents of those addresses has changed how I expect right?
    The contents of the objects at those addresses have changed, yes.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    Thanks... Pointers give me a headache!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM