我的char指针被gcc解释为整数

时间:2016-10-13 12:19:23

标签: c

我一直收到错误,我不知道为什么。由于某种原因,编译器认为char指针p是一个整数。我的代码的相关部分是:

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

从这段代码中,我编译并得到错误:

  

findkey.c:在函数'main'中:

     

findkey.c:25:11:警告:赋值使用整数指针   没有演员

     

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

     

findkey.c:顶级:

     

findkey.c:65:8:错误:'nextword'char * nextword(FILE * fd)的冲突类型

     

findkey.c:25:13:注意:先前隐含的'nextword'声明是   这里((p = nextword(fd))!= NULL)

1 个答案:

答案 0 :(得分:2)

由于在 main之后声明/定义nextword,编译器将其签名默认为int nextword(void)

在main之前定义它或在main之前添加前向声明:

char * nextword(FILE * fd);

当然,打开警告并实际上读取修复它们没有比这更好的了。

gcc -Wall -Werror -c file.c