Thread: Storing different values of time

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    23

    Storing different values of time

    Hello,
    I have a function which puts a timestamp into an array location.
    The code is as follows:
    Code:
    time_t curtime;
    curtime = time(NULL);
    array[i] = ctime(&curtime);
    The problem is that whenever I enter a new timestamp into a new array element, all the previous timestamps in the array are changed to this new value.
    Why does it do this, and how would I go about fixing it?
    Thanks.

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by stellastarr
    Hello,
    I have a function which puts a timestamp into an array location.
    The code is as follows:
    Code:
    time_t curtime;
    curtime = time(NULL);
    array[i] = ctime(&curtime);
    The problem is that whenever I enter a new timestamp into a new array element, all the previous timestamps in the array are changed to this new value.
    Why does it do this, and how would I go about fixing it?
    Thanks.
    first of all the ctime fucntion returns an char pointer that is a string. not an integer pointer. so when u do this
    Code:
    array[i] = ctime(&curtime);
    the pointer is pointing to a one single element or location in your array. before that can i see you array decalration. got to see its a string or just and array of diferent datatype. if u still follwing the above style then the string should be 2D string.

    if its a string then u need to store the first time and then second one has to be appened to the same string using some lib fucntion
    like strcat.

    ssharish2005

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    23
    Hello,
    I actually have an array inside a struct...
    Code:
    struct foo{
    ...
      char *date_created;
      char *date_modified;  
    };
    
    Then in my main I initiliaze like so:
    struct foo* f = (struct foo*)malloc(10*sizeof(struct foo)); to give me a struct with 10 locations...
    
    Then in my function as previously stated I do
    void fcn(struct foo** fooo)
    {
       struct foo* f = *fooo;
    
       time_t curtime;
       curtime = time(NULL);
    
       f[i].date_created = ctime(&curtime); // where i is a global variable
    ...
    }
    I hope this clarifies things.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    ctime returns a pointer to a static char array, so each call will return a pointer to the same location. If you want to keep track of separate times, you will need char arrays in your struct and use strcpy to copy the contents.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing values into a dynamic array
    By porsche911nfs in forum C++ Programming
    Replies: 5
    Last Post: 04-24-2009, 09:08 AM
  2. Need help with time
    By Gong in forum C++ Programming
    Replies: 7
    Last Post: 01-11-2007, 02:43 PM
  3. Killing someones grandparents
    By nickname_changed in forum A Brief History of Cprogramming.com
    Replies: 37
    Last Post: 09-07-2003, 07:56 AM
  4. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 10:12 PM