Results 1 to 8 of 8

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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Athens - Greece
    Posts
    219
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    3
    Thanked 1 Time in 1 Post

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

    See the code below you'll get some usefull indications on what to do:
    Qt Code:
    1. time_t timval=time(NULL);
    2. struct tm tims=*(localtime(&timval));
    3. char temp[100];
    4. int year;
    5.  
    6. year=1900+tims.tm_year;
    7.  
    8. sprintf(temp,"%02d/%02d/%04d - %02d:%02d:%02d",
    9. tims.tm_mday,tims.tm_mon+1,year,
    10. tims.tm_hour,tims.tm_min,tims.tm_sec);
    To copy to clipboard, switch view to plain text mode 
    Why don' you use QDateTime insread?
    Last edited by yop; 8th January 2006 at 09:46.

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

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

    Quote Originally Posted by yop
    struct tm tims=*(localtime(&timval));
    This means that localtime returns a pointer, which is dereferenced by the * and the value stored in the variable tm. And &timval corresponds to const time_t *tp.

    So this means that whenever I see a * inside the argument part of a function declaration, I must understand that that function must be passed a pointer, and the variables so preceded by stars are handled as pointers within the function.

    Similarly whenever I see a * before the function name in the function declaration, that function returns a pointer.

    Why don' you use QDateTime insread?
    Trying to do some pure ANSI C first before jumping into Qt...

    BTW can I know what the meaning of the 1136698157 number is?
    Penguin #395953 using Qt for open-source development on X11 using C++ and
    Python via PyQt

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

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

    I tried using
    Qt Code:
    1. struct tm now = *localtime(&time(NULL));
    To copy to clipboard, switch view to plain text mode 
    instead of
    Qt Code:
    1. time_t timval = time(NULL); struct tm now = *localtime(&timval);
    To copy to clipboard, switch view to plain text mode 
    and got an error:
    error: invalid lvalue in unary '&'
    Why?
    Penguin #395953 using Qt for open-source development on X11 using C++ and
    Python via PyQt

  4. #4
    Join Date
    Jan 2006
    Location
    Shanghai, China
    Posts
    52
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    3

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

    Quote Originally Posted by jamadagni
    I tried using
    Qt Code:
    1. struct tm now = *localtime(&time(NULL));
    To copy to clipboard, switch view to plain text mode 
    instead of
    Qt Code:
    1. time_t timval = time(NULL); struct tm now = *localtime(&timval);
    To copy to clipboard, switch view to plain text mode 
    and got an error: Why?
    What time(NULL) returns is not a left value so cannot be 'get-addressed'
    , coz the return value is a temporary value and will be destroyed after the statement.

    BTW can I know what the meaning of the 1136698157 number is?
    read the manual please
    1. Users don't have the manual, and if they did, they wouldn't read it.
    2. In fact, users can't read anything, and if they could, they wouldn't want to.

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
  •  
Qt is a trademark of The Qt Company.