2016-10-13 3 views
-1

私はエラーが発生し続けています。何らかの理由で、コンパイラはcharポインタpが整数だと考えます。私のコードの関連セクションは次のとおりです。私のcharポインタはgccによって整数として解釈され続けます

このコードから
int main(int argc, char **argv) { 
    char *p; 
    FILE *fd = fopen("words.txt", "r"); 
    unsigned char *iv = (unsigned char *)"0000000000000000"; 
    unsigned char *plaintext = (unsigned char *)"This is a top secret"; 
    unsigned char *real_ciphertext = (unsigned char *)"8d20e5056a8d24d0462ce74e4904c1b513e10d1df4a2ef2ad4540fae1ca0aaf9"; 
    //unsigned char key[128]; 
    unsigned char ciphertext[128]; 
    int ciphertext_len; 

    while ((p = nextword(fd)) != NULL) {  
     int l; 
     /* append space characters to key to make sure it is 16 characters long*/ 
     while ((l = strlen(p)) < 16) { 
      p[l + 1] = ' '; 
      p[l + 2] = '\0'; 
     } 
     /* Encrypt the plaintext with the key */ 
     ciphertext_len = encrypt(plaintext, strlen((char *)plaintext), p, iv, ciphertext); 

     if (strcmp(real_ciphertext, ciphertext) == 0) 
      printf("The key is:%s", p);   
    } 

    EVP_cleanup(); 
    ERR_free_strings(); 
    fclose(fd); 
} 

// 
// gets the next word from the list of words 
// 
#define MAXWORD 200 
// It returns the next word nullterminated from fd. 
// If there are no more more words it returns NULL. 
unsigned char word[MAXWORD]; 
unsigned char c; 

char *nextword(FILE *fd) { 
    int wordLength = 0; 

    while ((c = fgetc(fd)) != EOF) { 
     if ((c != ' ') && (c != '\n') && (c !='\t') && (c !='\r')) { 
      word[wordLength] = c; 
      wordLength++; 
     } else 
     if (wordLength > 0) { 
      word[wordLength] = '\0';   
      return word; 
     } 
    } 

    if (wordLength > 0) { 
     word[wordLength] = '\0'; 
     return word; 
    } else 
     return NULL; 
} 

、私はコンパイルしてエラーを取得:nextword以来

findkey.c: In function 'main':

findkey.c:25:11: warning: assignment makes pointer from integer without a cast

while ((p = nextword(fd)) != NULL)

findkey.c: At top level:

findkey.c:65:8: error: conflicting types for 'nextword' char *nextword(FILE *fd)

findkey.c:25:13: note: previous implicit declaration of 'nextword' was here while ((p = nextword(fd)) != NULL)

+0

'main()'の前に* 'nextword()'を宣言する必要があります。さもなければ、 'int'を返すと仮定します。そして、あなたは後でエラーを受け取ります(これはあなたが読んでいたはずです)。 – Biffen

+3

警告をオンにし、コンパイラを正しく設定してください。これは純粋にあなたのツールを聞くことで解決された可能性があり、人間の介入は必要ありませんでした。 –

+0

すべての警告を有効にしてコンパイルします。 –

答えて

2

を/宣言メイン、コンパイラのデフォルトにその署名後を定義していますint nextword(void)

どちらのメインの前にそれを定義したり、main前に前方宣言を追加します。

当然の
char * nextword(FILE * fd); 

は、何も警告をオンにする方が良いではありませんし、実際に修正それらを読んで。

gcc -Wall -Werror -c file.c 
+0

また、このエラーの診断に役立つコンパイラの警告を有効にすることをお勧めします。 – chqrlie

関連する問題