ANSI C / assignment makes pointer from integer

Andreas Jaeger aj@suse.de
Tue Aug 7 06:31:00 GMT 2001


Sebastian Ude <ude@handshake.de> writes:

> I *did* include string.h.

But strdup is not part of ANSI C, check:

gee:~/tmp:[0]$ gcc -Wall -ansi str.c
str.c: In function `main':
str.c:5: warning: implicit declaration of function `strdup'
str.c:5: warning: initialization makes pointer from integer without a cast
str.c:5: warning: unused variable `s'

implicit declared functions return int...

Andreas

> Guess why I placed the
>
> [...]'s
>
> Yep, to tell you that I left out some lines of code which seemed natural to
> me.
>
> Try this complete example:
>
> #include <string.h>
>
> int main(void)
>   {
>      char *s = strdup("foo");
>      return 0;
>   }
>
> string.h is included, so the definition of strdup() should be present.
> But the warning is still there when compiling with -ansi ...
>

-- 
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE7b+2bOJpWPMJyoSYRAqnAAJoDR2ntkua8jpoqaKlkm7U47tFCgQCfSXNM
qH7WkRMRY6bSZfrEMBGDMn4=
=mjeE
-----END PGP SIGNATURE-----From ude@handshake.de Tue Aug 07 06:37:00 2001
From: Sebastian Ude <ude@handshake.de>
To: fjh@cs.mu.oz.au
Cc: gcc@gcc.gnu.org
Subject: Re: ANSI C / assignment makes pointer from integer
Date: Tue, 07 Aug 2001 06:37:00 -0000
Message-id: <20010807133503.9AD5096D22@sender.ngi.de>
References: <20010807223911.A28557@hg.cs.mu.oz.au>
X-SW-Source: 2001-08/msg00432.html
Content-length: 1104

On Tue, 7 Aug 2001, fjh@cs.mu.oz.au (Fergus Henderson) wrote:
> Date: Tue, 7 Aug 2001 22:39:11 +1000
> To: Bohdan Vlasyuk <bohdan@kivc.vstu.vinnica.ua>
> From: fjh@cs.mu.oz.au (Fergus Henderson)
> CC: Sebastian Ude <ude@handshake.de>, gcc@gcc.gnu.org
> Subject: Re: ANSI C / assignment makes pointer from integer

[...]

> Actually it's a bit more complicated than that.
> strdup() is a non-standard function, so as well as #including <string.h>,
> you also need to compile with the right options; if you compile with
> `ansi', then strdup() will not be declared even if you #include
> <string.h>.
> 
> If you want to write standard-conforming code, don't use strdup().
> 
> The simplest solution is to write a version of strdup() yourself,
> name it something different, e.g. `sebastian_strdup()', and use that.

That's intresting.
In fact, the strdup manual page says that it's a BSD / SVID invention, but
it seems not to be defined through the ISO / ANSI C standard.

Compiling with -Wimplicit-function-declaration made the whole situation a
lot clearer - why isn't this warning enabled by default ?



More information about the Gcc mailing list