2

Aargh! Why is the compiler complaining? Thanks for any help!

% gcc -o mine mine.c -lcrypt
mine.c: In function 'main':
mine.c:19:14: warning: assignment makes pointer from integer without a cast [enabled by default]
%

Code:

#define _X_OPEN_SOURCE
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int main() {

    const char key[] = "hello world";
    const char salt[]="01";
    const int MAXIMUM_HASH_LENGTH = 2 + 11;
    int i=0;

    char *hash;

    long long hashes = 0L;

    while (1) {

        hash = crypt(key, salt); /* problem with this line... */
        if (hash[2] == '0') {
            int leading0s = 0;
            leading0s++;
            for (i=3; i < MAXIMUM_HASH_LENGTH; i++) {
                if (hash[i] != '0') break;

                leading0s++;
            }
            printf("Winner: %s has %d leading zeros.\n",
                    hash, leading0s);
            printf("\t--> Hash %lld.\n\n", hashes);
        }

        if (hashes != 0 && (hashes % 10000) == 0) {
            printf("Hash %d: %s\n", hashes, hash);
        }

        if (hashes== 1000000) break;
        hashes++;

    }

    return 1000;

}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
user1505713
  • 615
  • 5
  • 20
  • 4
    You're compiling this on a platform where `crypt()` is not declared, at least not in the header files you have included. But really, would it be hard to google the compiler warning message? –  Jul 07 '13 at 05:43
  • 1
    I looked here -- http://www.gnu.org/software/libc/manual/html_node/crypt.html -- and could not find significant differences. And the man page says to include along with the #define above it. – user1505713 Jul 07 '13 at 05:47

2 Answers2

5

Try changing "#define _X_OPEN_SOURCE" to "#define _XOPEN_SOURCE".

Dan D.
  • 73,243
  • 15
  • 104
  • 123
vinayak
  • 86
  • 2
2

You should normally specify a number for _XOPEN_SOURCE; valid values include:

/*
** Include this file before including system headers.  By default, with
** C99 support from the compiler, it requests POSIX 2001 support.  With
** C89 support only, it requests POSIX 1997 support.  Override the
** default behaviour by setting either _XOPEN_SOURCE or _POSIX_C_SOURCE.
*/

/* _XOPEN_SOURCE 700 is loosely equivalent to _POSIX_C_SOURCE 200809L */
/* _XOPEN_SOURCE 600 is loosely equivalent to _POSIX_C_SOURCE 200112L */
/* _XOPEN_SOURCE 500 is loosely equivalent to _POSIX_C_SOURCE 199506L */

#if !defined(_XOPEN_SOURCE) && !defined(_POSIX_C_SOURCE)
#if __STDC_VERSION__ >= 199901L
#define _XOPEN_SOURCE 600   /* SUS v3, POSIX 1003.1 2004 (POSIX 2001 + Corrigenda) */
#else
#define _XOPEN_SOURCE 500   /* SUS v2, POSIX 1003.1 1997 */
#endif /* __STDC_VERSION__ */
#endif /* !_XOPEN_SOURCE && !_POSIX_C_SOURCE */

You may prefer to use 700 instead of 600; it depends on your platform(s). However, with the correct spelling, you can simply write #define _XOPEN_SOURCE and it will also define the crypt() for you.


You might also care to note that the exit status is limited to an 8-bit value, so returning 1000 from main() is equivalent to returning 1000 % 256. You also need to fix line 34 of your code:

        printf("Hash %d: %s\n", hashes, hash);

should be:

        printf("Hash %lld: %s\n", hashes, hash);
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • What does that mean? :O, so I should declare main `char main(void)` instead of `int main()`. Can you please extend a little bit more about this?, maybe some link I could use to further reading. – yeyo Jul 07 '13 at 06:14
  • It means you should declare `int main(void)` or `int main(int argc, char **argv)`, but the value returned from `main()` will be treated as an 8-bit value when it is built into the status value returned to the parent process via [`wait()` or `waitpid()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/waitpid.html) or their relatives. See [Exit codes bigger than 255 — possible?](http://stackoverflow.com/questions/179565/exitcodes-bigger-than-255-possible/179652#179652) for an explanation. – Jonathan Leffler Jul 07 '13 at 06:16