-1

I keep getting an error, and I'm not sure why. For some reason the compiler thinks the char pointer p is an integer. The relevant section of my code is:

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

From this code, I compile and get the error:

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)

chqrlie
  • 131,814
  • 10
  • 121
  • 189
fdzsfhaS
  • 79
  • 10
  • You need to *declare* `nextword()` before `main()`. Otherwise it's assumed to return an `int`, and you get an error later (which you should have read). – Biffen Oct 13 '16 at 12:21
  • 3
    Turn on your warnings and configure your compiler properly. This could have been resolved purely by listening to your tools, and didn't need human intervention. – Kerrek SB Oct 13 '16 at 12:24
  • Compile with all warning enabled. – Jabberwocky Oct 13 '16 at 12:24

1 Answers1

2

Since nextword is declared/defined after main, the compiler defaults its signature to int nextword(void)

Either define it before main or add a forward declaration before main:

char * nextword(FILE * fd);

of course, nothing is better that turning on the warnings and actually read and fix them.

gcc -Wall -Werror -c file.c
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • You might also suggest turning on compiler warnings that would have helped diagnose this error. – chqrlie Oct 14 '16 at 05:40