crypt():没有强制转换的整数指针?

时间:2013-07-07 05:41:16

标签: c function

AARGH!为什么编译器会抱怨?谢谢你的帮助!

% 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]
%

代码:

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

}

2 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:2)

您通常应为_XOPEN_SOURCE指定一个数字;有效值包括:

/*
** 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 */

您可能更喜欢使用700而不是600;这取决于您的平台。但是,如果拼写正确,您只需撰写#define _XOPEN_SOURCE,它也会为您定义crypt()


您可能还需要注意,退出状态仅限于8位值,因此从main()返回1000相当于返回1000 % 256。您还需要修复代码的第34行:

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

应该是:

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