ccastingcharformat

What do I do to stop an error "expected expression before ‘char’"?


I'm compiling my work and this error kept on appearing no matter how I edit my code:

expected expression before ‘char’

and

format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’

as of the second error, I've tried to use typecasting but the problem is really persistent. Does anyone know how to? This is a part of my code:

while ( char my_wget (char web_address[BUFLEN]) != EOF ) {
    printf ("%s", (char) web_address[BUFLEN]);

Solution

  • Because you've got a syntax error where you wrote char and char is not allowed.

    Maybe you had in mind:

    int ch;
    char web_address[BUFLEN];
    
    while ((ch = my_wget(web_address)) != EOF)
        printf("%s\n", web_address);
    

    With the correct declaration for my_wget() around (such as extern int my_wget(char *buffer);), that should compile. You can't declare variables everywhere.

    The second error is because web_address[BUFLEN] is a character (certainly in my code; it seems to be in yours, too, since the compiler managed to identify the type sufficiently to generate the error). It is also one beyond the end of the array if you declared it as I did. Treating a char value (probably an 8-bit quantity) as an address (pointer; probably a 32-bit or 64-bit quantity) is wrong, which is why the compiler complained.