Sakamoto
Sakamoto

Reputation: 155

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]);

Upvotes: 0

Views: 1807

Answers (3)

Barath Ravikumar
Barath Ravikumar

Reputation: 5836

See this code below

#include <stdio.h>

int main()
{
char c;
printf ("%s",  st);
}

when i compile it , i get the same warning message.

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

so i change the program to

#include <stdio.h>

int main()
{
char *str = "string";
printf ("%s",  st);
}

And now the program compiles properly.

So being a newcomer to c , this is how you learn the language , write the smallest example , to prove that you have a firm grip over the concept.

Upvotes: 0

user2050932
user2050932

Reputation:

In the printf() statement, try changing the part char to char* Same applies to the condition in the while loop. Change the char before web_address to (char*)

I find it weird that you write "char" before my_wget(). Can you please be more specific?

Upvotes: 1

Jonathan Leffler
Jonathan Leffler

Reputation: 754090

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.

Upvotes: 2

Related Questions