C, conflicting types on a function that takes in chars and returns chars when it's been declared already? Then sudden loop only supported in c99

Viewed 56

I am getting these two following error messages on 2 parts of the code that's been marked as comments.

But why do these error messages happen? I have declared the decrypt function before main haven't I? and is this not how you implement a for loop in C?

#include <stdio.h>
#include <stdlib.h>
#include <conf.h>
#include <evp.h>
#include <err.h>
#include <string.h>
#include <ctype.h>
unsigned char *readWord(FILE *fp);
char *decrypt(unsigned char *Ciphertext, unsigned char *key,unsigned char *iv);
int main (void)
{
    char *key = NULL;
    char *iv = "aabbccddeeff00998877665544332211";
    char *Plaintext = "This is a top secret.";
    char *Ciphertext = "764aa26b55a4da654df6b19e4bce00f4ed05e09346fb0e762583cb7da2ac93a2";
    char *checker;

    FILE *words = (fopen("words", "r"));

    while(1) {
        key = readWord(words);
        if (!key){
            printf("the key was not found");
            break;
        }
        else{
            checker = decrypt(Ciphertext,key,iv);
            if(strcmp(checker,Plaintext)){
                printf("the correct key is ");
                printf(key);
            }
            free(key);
            free(checker);

        }
    }

    return 0;
}

char *decrypt(unsigned char *Ciphertext, unsigned char *key, unsigned char *iv) //here it's correctly declared right?
{
    EVP_CIPHER_CTX *ctx; //could not be resolved not sure why
    unsigned char *plainText = malloc(sizeof(unsigned char) * 64);

    int len;
    int plaintext_len;

    if(!(ctx = EVP_CIPHER_CTX_new()))
        handleErrors(); //why error here it's fine below


    if(1 != EVP_DecryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv))

        handleErrors();


    if(1 != EVP_DecryptUpdate(ctx, plainText, &len, Ciphertext, plaintext_len))
        handleErrors();
        plaintext_len += len;


    if(1 != EVP_DecryptFinal_ex(ctx, plainText + len, &len))
        handleErrors();
    plaintext_len += len;

    EVP_CIPHER_CTX_free(ctx);

    int checkLen = (2 * len) + 1;
    char *checker2 = malloc(sizeof(char) * (checkLen));
    for(int i = 0; i < len; ++i) //for loop only allowed in C99 mode what?
    {
        sprintf(checker2 + (i*2),"%02x",plainText[i]);
    }
    free(plainText);


    return checker2;
}

This is the complete error messages on the console.

Info: Configuration "Debug" uses tool-chain "Cygwin GCC" that is unsupported on this system, attempting to build anyway.
make all 
'Building file: ../src/try.c'
'Invoking: Cygwin C Compiler'
gcc -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/try.d" -MT"src/try.o" -o "src/try.o" "../src/try.c"
../src/try.c: In function 'main':
../src/try.c:30:10: warning: pointer targets in assignment differ in signedness [-Wpointer-sign]
../src/try.c:36:7: warning: pointer targets in passing argument 1 of 'decrypt' differ in signedness [-Wpointer-sign]
../src/try.c:18:7: note: expected 'unsigned char *' but argument is of type 'char *'
../src/try.c:36:7: warning: pointer targets in passing argument 2 of 'decrypt' differ in signedness [-Wpointer-sign]
../src/try.c:18:7: note: expected 'unsigned char *' but argument is of type 'char *'
../src/try.c:36:7: warning: pointer targets in passing argument 3 of 'decrypt' differ in signedness [-Wpointer-sign]
../src/try.c:18:7: note: expected 'unsigned char *' but argument is of type 'char *'
../src/try.c: In function 'decrypt':
../src/try.c:59:9: warning: implicit declaration of function 'handleErrors' [-Wimplicit-function-declaration]
../src/try.c:67:5: warning: pointer targets in passing argument 2 of 'EVP_DecryptUpdate' differ in signedness [-Wpointer-sign]
In file included from ../src/try.c:13:0:
c:\users\marcu\gcc\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/../../../../x86_64-w64-mingw32/include/evp.h:635:5: note: expected 'unsigned char *' but argument is of type 'char *'
../src/try.c:72:5: warning: pointer targets in passing argument 2 of 'EVP_DecryptFinal_ex' differ in signedness [-Wpointer-sign]
In file included from ../src/try.c:13:0:
c:\users\marcu\gcc\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/../../../../x86_64-w64-mingw32/include/evp.h:638:5: note: expected 'unsigned char *' but argument is of type 'char *'
../src/try.c:80:5: error: 'for' loop initial declarations are only allowed in C99 mode
../src/try.c:80:5: note: use option -std=c99 or -std=gnu99 to compile your code
make: *** [src/try.o] Error 1
"make all" terminated with exit code 2. Build might be incomplete.

1 Answers

This happens because declaring variables inside a for loop wasn't valid C until C99(which is the standard of C published in 1999), you can either declare your counter outside the for as pointed out by others or use the -std=c99 flag to tell the compiler explicitly that you're using this standard and it should interpret it as such.

You can init i variable like this.

    int i;
    for(i = 0; i < len; ++i) //for loop only allowed in C99 mode what?
    {
        sprintf(checker2 + (i*2),"%02x",plainText[i]);
    }
Related