2

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

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

}
4

2 回答 2

5

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

于 2013-07-07T06:03:48.220 回答
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 位值,因此返回 1000 frommain()等同于返回1000 % 256。您还需要修复代码的第 34 行:

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

应该:

        printf("Hash %lld: %s\n", hashes, hash);
于 2013-07-07T06:07:41.337 回答