简体   繁体   中英

crypt(): Pointer to integer without cast?

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;

}

尝试将“ #define _X_OPEN_SOURCE ”更改为“ #define _XOPEN_SOURCE ”。

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM