From owner-freebsd-questions@FreeBSD.ORG Tue Aug 10 16:45:27 2004 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 2B36516A4D5 for ; Tue, 10 Aug 2004 16:45:27 +0000 (GMT) Received: from ux11.ltcm.net (ux11.ltcm.net [64.215.98.6]) by mx1.FreeBSD.org (Postfix) with ESMTP id 6301843D1D for ; Tue, 10 Aug 2004 16:45:26 +0000 (GMT) (envelope-from mipam@ibb.net) Received: from ux11.ltcm.net (mipam@localhost.ltcm.net [IPv6:::1]) by ux11.ltcm.net (8.12.9/8.12.9/UX11TT) with ESMTP id i7AGjLIo014182; Tue, 10 Aug 2004 18:45:21 +0200 (MEST) Received: from localhost (mipam@localhost) by ux11.ltcm.net (8.12.9/8.12.9/Submit) with ESMTP id i7AGjJTK003800; Tue, 10 Aug 2004 18:45:20 +0200 (MEST) X-Authentication-Warning: ux11.ltcm.net: mipam owned process doing -bs Date: Tue, 10 Aug 2004 18:45:19 +0200 (MEST) From: Mipam X-X-Sender: mipam@ux11.ltcm.net To: Giorgos Keramidas In-Reply-To: <20040810162612.GC25389@orion.daedalusnetworks.priv> Message-ID: References: <20040810162612.GC25389@orion.daedalusnetworks.priv> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII cc: freebsd-questions@freebsd.org Subject: Re: localtime question X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 10 Aug 2004 16:45:27 -0000 > Note that it's a good idea to explicitly specify that main() does > nothing to its argument with: > > int > main(void) > { > ... > } > > and that main() has to `return' some value that fits in an `int' as its > definition says it should. > > > But at compilation it complains: > > > > tijd.c: In function `main': > > tijd.c:15: warning: passing arg 1 of `ctime' makes pointer from integer without a cast > > > > What am i doing wrong? > > You're passing the "value" of tv.tv_sec in a function that expects a > pointer to it. > > % man ctime > > char * > ctime(const time_t *clock); > > The value you passed to ctime() is not an address of a constant time_t > object but the value of the time_t object itself. The compiler detects > that a conversion from an integral type to a pointer needs to take place > and warns you about it. > > This is not a fatal error (c.f. the rationale of the ANSI C99 standard, > which specifies that pointers might be converted to integral values and > back to the same pointer type without losing any information). However, > it is usually a very common mistake (forgetting to pass a reference to > the object and passing the object itself). This is why you get warned. > > Try calling ctime() with the address of tv.tv_sec: > > printf("%s\n", ctime(&tv.tv_sec)); #include #include int main(void) { struct timeval tv; struct timeval tv_current; if (gettimeofday(&tv_current, NULL) == -1) err(1, "Could not get local time of day"); tv.tv_sec = tv_current.tv_sec-86400; printf("%s\n", ctime(&tv.tv_sec)); return 0; } Does the job, thanks! At compile time i get: tijd.c: In function `main': tijd.c:11: warning: passing arg 1 of `ctime' from incompatible pointer type But it works: Mon Aug 9 18:44:04 2004 nicely the time of yesterday. Only thing left is another format like: 2004 Aug 9 18:44:04 Any hints? Bye, Mipam.