[Techtalk] getting the time as a string in c
John Stoneham
lyric.techtalk at lyrically.net
Fri Sep 30 01:27:15 EST 2005
> > I thought that maybe I could replace it with a space, using something like
> > this:
> >
> > time_t timeseconds;
> > timeseconds = time(NULL);
> > char timestring[] = ctime(×econds); /* line 13 */
> > timestring[strlen(timestring)-1] = " ";
> > printf("%s", ctime(&t));
>
> If you declare timestring as "char * timestring = ...."; You'll be fine.
Exactly. The reason the compiler's complaining is because you're declaring an
array with no length, which isn't allowed. The return type of ctime() is
"char *", so that's what you'll need to use. In C, using the name of an array
is synonymous with using a pointer to the first element - there's no
bounds-checking on your array access like there is in perl. The statements
"char timestring[10]" and "char *timestring" both declare timestring as a
pointer to a character, but the first one also allocates the memory for the
array on the stack.
- John
More information about the Techtalk
mailing list