Thread: struct tm confusion?

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    24

    struct tm confusion?

    I am attempting to write a alarm clock which receives input from the user. My initial design was to have the user fill out the tm structure by hand. I found two different examples of this and am a little confused as to which I should use.

    The first-
    struct tm str_time;

    str_time.tm_year = 2012-1900;
    str_time.tm_mon = 6;
    str_time.tm_mday = 5;
    str_time.tm_hour = 10;
    str_time.tm_min = 3;
    str_time.tm_sec = 5;
    str_time.tm_isdst = 0;

    And the second-

    struct tm * timeinfo;

    timeinfo = localtime ( &rawtime );
    timeinfo->tm_year = year - 1900;
    timeinfo->tm_mon = month - 1;
    timeinfo->tm_mday = day;

    My main source of confusion is whether I should use the ->, or the str_time.blank method?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Have you looked at the man pages to see what the localtime function does? You should, and see whether that's what you want.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    Quote Originally Posted by deadrabbit View Post
    My main source of confusion is whether I should use the ->, or the str_time.blank method?
    Yes, you should definitely use one of these two options.

    Your confusion is that one is example is defined as a struct so members are accessed using dot. The other one is a pointer to a structure, where members are accessed using -> . In your case I'd imagine you'd want the first one - if you use the second it only allocates something which points at the structure instead of allocating a structure itself to fill in.

    But as tabstop said, that would depend on the rest of your program.

    PS - Note that you can also use . to access members if you have a pointer to a structure, but the syntax is a bit more ugly : (*timeinfo).tm_year Here you're first using the * to find what timeinfo points to. Then you're grabbing the tm_year field from the dereferenced pointer. You need the parens because in the order of operations . happens first if you don't. This is ugly and no fun to type, so everyone uses -> instead. But compare the two and you'll see there's only one difference :

    ( str_time).tm_year
    (*timeinfo).tm_year

    Including all the optional stuff to make them as close as possible, the * is the only difference between the two, and it's used on pointers only.

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by KCfromNC View Post
    Yes, you should definitely use one of these two options.

    Your confusion is that one is example is defined as a struct so members are accessed using dot. The other one is a pointer to a structure, where members are accessed using -> . In your case I'd imagine you'd want the first one - if you use the second it only allocates something which points at the structure instead of allocating a structure itself to fill in......
    Actually, those code snippets do different things, hence tabstop's post. You may have missed the localtime() function call however I feel obligated to refer you to:
    The localtime() function
    tm structure

    @OP: Like tabstop said, what is it you are trying to accomplish?
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    24
    Thanks KCfromNC, that helped to clarify things for me somewhat

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 05-12-2011, 01:02 AM
  2. Struct and array! confusion
    By ronrardin in forum C Programming
    Replies: 5
    Last Post: 04-05-2010, 12:09 PM
  3. C struct / pointer confusion
    By ChipsHandon in forum C Programming
    Replies: 5
    Last Post: 02-26-2010, 02:08 AM
  4. struct holding data inside a linked list struct
    By icestorm in forum C Programming
    Replies: 2
    Last Post: 10-06-2009, 12:49 PM
  5. Pointer + struct w/arrays confusion
    By Viper187 in forum C Programming
    Replies: 1
    Last Post: 07-23-2009, 09:37 AM