2013-07-07 2 views
2

아아! 컴파일러가 불평하는 이유는 무엇입니까? 어떤 도움을 주셔서 감사합니다!crypt() : 캐스트가없는 정수에 대한 포인터?

% 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

당신은 적어도하지에'토굴()가'선언되지 않은 플랫폼에서이 컴파일하고 또한 코드의 라인 (34)을 수정해야 포함 된 헤더 파일. 하지만 실제로 컴파일러 경고 메시지를 Google에 전달하는 것이 어려울까요? –

+1

나는 여기를 보았다 - http://www.gnu.org/software/libc/manual/html_node/crypt.html - 중요한 차이점을 발견 할 수 없었다. 맨 페이지에는 #define과 함께 이 포함되어 있습니다. – user1505713

답변

5

"#define _XOPEN_SOURCE"에 "#define _X_OPEN_SOURCE"를 변경해보십시오.

+0

LOL 나는 그것을 제안하려고했다. – yeyo

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

600 대신 700을 사용하는 것이 좋습니다. 귀하의 플랫폼에 따라 다릅니다. 그러나 올바른 맞춤법을 사용하면 #define _XOPEN_SOURCE을 쓸 수 있으며 crypt()을 정의 할 수도 있습니다.


또한 너무 main()에서 1000을 반환, 종료 상태는 8 비트 값으로 제한되어 있음을주의하는 신경 수는 1000 % 256를 반환하는 것과 동일합니다.

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

가 있어야한다 :

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

그게 무슨 뜻입니까? : O, 그래서'int main()'대신 main'char main (void)'를 선언해야합니다. 이 문제에 대해 좀 더 자세히 설명해 주시겠습니까? 아니면 더 읽을 거리가있는 링크 일 수도 있습니다. – yeyo

+0

'int main (void)'또는'int main (int argc, char ** argv)'를 선언해야하지만,'main()'에서 반환 된 값은 8 비트 값으로 취급됩니다. ['wait()'또는'waitpid()'] (http://pubs.opengroup.org/onlinepubs/9699919799/functions/waitpid.html) 또는 그 친척을 통해 부모 프로세스에 반환 된 상태 값에 내장됩니다. 자세한 내용은 [255보다 큰 종료 코드 - 가능 여부] (http://stackoverflow.com/questions/179565/exitcodes-bigger-than-255-possible/179652#179652)를 참조하십시오. –

+0

많은 감사, Jonathan Leffler. – yeyo

관련 문제