Results 1 to 8 of 8

Thread: Problem with pointers while using localtime() and time()

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    11 N 78 E
    Posts
    110
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Problem with pointers while using localtime() and time()

    Basically what I want to do is get the current year, month, day, hour, minute and second.

    I know I should use first time() and then pass the result to localtime() after which I can extract the desired values from the members of the tm struct. But the pointers involved in between have me muddled. The following are the declarations of the time() and localtime() functions:
    Qt Code:
    1. time_t time(time_t *tp);
    2. struct tm *localtime(const time_t *tp);
    To copy to clipboard, switch view to plain text mode 
    Now I am confused - If I get it correctly, time(time_t *tp) means that the function time gets some argument, which is equated to time_t *tp. That means (to my foggy brain) that tp is a pointer to a value of type time_t. The documentation says that the current calendar time is returned, and if tp is not NULL then the current calendar time is also assigned to *tp.

    But I do not understand what struct tm *localtime means. Does it mean that localtime returns a pointer? And localtime(const time_t *tp) means that I have to give it a value or pointer (using &) as input? Please explain.

    Turning to the practical details, I have successfully implemented:
    Qt Code:
    1. time_t now;
    2. now = time(NULL);
    3. printf("%d\n",now);
    To copy to clipboard, switch view to plain text mode 
    whereby I get results like 1136698157 etc. Now I would like to know what this number means.

    Also, I face problems when I try to convert the time_t value to a struct tm variable using localtime. If I simply do a
    Qt Code:
    1. struct tm nowtm;
    2. nowtm = localtime(now);
    To copy to clipboard, switch view to plain text mode 
    I get a warning and error for this:
    time.c:12: warning: passing argument 1 of 'localtime' makes pointer
    from integer without a cast
    time.c:12: error: incompatible types in assignment
    So please tell me what I should do. Thanks for your patience and time.
    Last edited by jamadagni; 8th January 2006 at 07:56. Reason: Improve readability
    Penguin #395953 using Qt for open-source development on X11 using C++ and
    Python via PyQt

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.