1
#include <stdio.h>
#define _XOPEN_SOURCE
#include <unistd.h>

int main()
{
    const char *key = NULL;
    const char *salt = NULL;
    crypt(key, salt);
    return 0;
}

use gcc test.c -o test -Wall -lcrypt to compile.

Which gives this warning:

initialization makes pointer from integer without a cast

Can anyone explain this warning and how to properly avoid it?

Jeremy J Starcher
  • 23,369
  • 6
  • 54
  • 74
monkeyFly
  • 23
  • 6
  • 2
    Can you include the line number in the warning message please ? – Paul R Aug 15 '14 at 10:06
  • 3
    @KlasLindbäck: no, `crypt()` should be defined in ``. – Paul R Aug 15 '14 at 10:09
  • Though I needed to move #define to the first line to make the function declared, see http://stackoverflow.com/questions/6127921/is-the-crypt-function-declared-in-unistd-h-or-crypt-h – wRAR Aug 15 '14 at 10:10
  • 3
    I wonder why your gcc didn't say "implicit declaration of function ‘crypt’", mine did. – wRAR Aug 15 '14 at 10:11
  • @PaulR The reference page I consulted included crypt.h explicitly instead of specifying _XOPEN_SOURCE. I got "implicit declaration of function ‘crypt’", just like wRAR. (Deleting my comment above, so as to not mislead future readers) – Klas Lindbäck Aug 15 '14 at 10:16
  • 2
    The line number gets increasingly interesting, because there are no integers at all in your code. – Jonas Schäfer Aug 15 '14 at 10:39
  • @KlasLindbäck: I think it depends on what flavour of Linux/Unix/whatever that you are using. For anything POSIX or BSD-based (e.g. Mac OS X) then it's ``, but it may be different on Linux, although [this man page](http://linux.die.net/man/3/crypt) suggests it's still ``. – Paul R Aug 15 '14 at 11:11
  • 1
    On my Linux (RHEL6) it works with `` if I first define _XOPEN_SOURCE. – Klas Lindbäck Aug 15 '14 at 12:07

1 Answers1

3

You have to put feature test macros before all includes. In your case, stdio.h already includes features.h behind the scenes, which takes care of converting the feature defines (like _XOPEN_SOURCE) into something internal the headers use. So when you include unistd.h, the flags have already been set and won’t be interpreted again, thus, declaring _XOPEN_SOURCE in the mean time won’t have any effect.

Changing the order fixes the problem:

#define _XOPEN_SOURCE
#include <stdio.h>
#include <unistd.h>

int main()
{
    const char *key = NULL;
    const char *salt = NULL;
    crypt(key, salt);
    return 0;
}
Jonas Schäfer
  • 20,140
  • 5
  • 55
  • 69
  • *..stdio.h already includes unistd.h behind the scenes.* any reference? – P.P Aug 15 '14 at 10:26
  • @KingsIndian You’re correct, it’s including ``features.h``, not ``unistd.h``. I corrected and completed my answer. Thanks for pointing this out. – Jonas Schäfer Aug 15 '14 at 10:33
  • 1
    While I see how this should fix problems with `crypt` prototype, I don't see how any problem with the prototype could cause the warning mentioned in the question. Because the warning says pointer _from_ integer and calling function without prototype can't cause that. – Jan Hudec Aug 15 '14 at 10:37
  • 2
    @JanHudec This is true. I misread that (probably like some others). I’d really like feedback from the asker whether this fixes the problem and if not, where the warning is coming from. – Jonas Schäfer Aug 15 '14 at 10:48
  • I guess the warning is coming from `unistd.h` when being compiled in a situation where `stdio.h` had already been compiled with possibly different function definitions – M.M Aug 15 '14 at 11:51
  • @MattMcNabb That sounds very plausible. – Jonas Schäfer Aug 15 '14 at 12:26