[LCP]time
Greg Black
gjb at gbch.net
Sat Jan 19 13:35:35 UTC 2002
Joachim Bauernberger wrote:
| I am having a little problem with time. For some reason the code below will
| have the following output:
[Erroneous data elided.]
| time_t now;
| struct tm *tt;
|
| tt = localtime(&now);
This is a common beginner's error which often stems from
careless reading of the man pages.
Certainly, localtime() needs a pointer to a time_t as you have
given it -- but the time_t variable must be initialised first so
that localtime() has something to work with. You need something
like this /before/ the call to localtime():
time(&now);
This will initialise "now" with the current time instead of the
random value it had in your original program, so localtime()
will have something meaningful to work with.
Almost always, when library man pages show a pointer argument to
a function, you need to think hard about what is meant and what
you need to do first.
Greg
More information about the linuxCprogramming
mailing list