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.
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?
Have you looked at the man pages to see what the localtime function does? You should, and see whether that's what you want.
Yes, you should definitely use one of these two options. :D
Originally Posted by deadrabbit
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 :
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.
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:
Originally Posted by KCfromNC
The localtime() function
@OP: Like tabstop said, what is it you are trying to accomplish?
Thanks KCfromNC, that helped to clarify things for me somewhat :cool: