From owner-freebsd-questions@FreeBSD.ORG Tue Aug 10 16:26:19 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 F02D416A4CE for ; Tue, 10 Aug 2004 16:26:19 +0000 (GMT) Received: from aiolos.otenet.gr (aiolos.otenet.gr [195.170.0.23]) by mx1.FreeBSD.org (Postfix) with ESMTP id 63F8043D48 for ; Tue, 10 Aug 2004 16:26:18 +0000 (GMT) (envelope-from keramida@ceid.upatras.gr) Received: from orion.daedalusnetworks.priv (aris.bedc.ondsl.gr [62.103.39.226])i7AGQFqo020975; Tue, 10 Aug 2004 19:26:16 +0300 Received: from orion.daedalusnetworks.priv (orion [127.0.0.1]) i7AGQDTf026044; Tue, 10 Aug 2004 19:26:13 +0300 (EEST) (envelope-from keramida@ceid.upatras.gr) Received: (from keramida@localhost)i7AGQCuY026043; Tue, 10 Aug 2004 19:26:12 +0300 (EEST) (envelope-from keramida@ceid.upatras.gr) Date: Tue, 10 Aug 2004 19:26:12 +0300 From: Giorgos Keramidas To: Mipam Message-ID: <20040810162612.GC25389@orion.daedalusnetworks.priv> References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: 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:26:20 -0000 On 2004-08-10 16:49, Mipam wrote: > Hi, > > I tried to display the time from yesterday by this little program > > #include > #include > > time_t tval; > > int main() > { > 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)); > } 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)); - Giorgos