user2638180 user2638180 - 1 month ago 8
C Question

Why can't I use a char as a parameter of a function?

I have the following function on my code:

int leepos(char *entrada, char elem)


Which is being called the following way:

leepos(entrada,'y');


For me it looks ok, but I get the following error: [Error] conflicting types for 'leepos'

Oddly enough, I can make my function work with these little modifications:

Defining the function as:

int leepos(char *entrada, char* elem)


And calling it with:

leepos(entrada,"y");


I can make it work properly once I change the logic that treated the char to treat strings.

It looks like the error is then in using char as a parameter, although it looks quite strange. Any idea of why this may happen?

I'm giving the code that shows the error is on using char:

int main()
{
leepos("hi",'y');
leepos2("hi","y");
}

int leepos(char *entrada, char elem)
{
return 0;
}

int leepos2(char *entrada, char* elem)
{
return 0;
}


The program works nice if I delete the line leepos("hi",'y');, the error is still the same as the one shown, so I think this makes clear the error has to be there.

M.M M.M
Answer

You need to prototype the function before calling it:

int leepos(char *entrada, char elem);

int main()
{
    leepos("hi",'y');
}

int leepos(char *entrada, char elem)
{
    return 0;
}

Another way is to put the whole function body before main (the first line serves as a prototype too).

Since C99 it is not allowed at all to call a function that has not been declared. The error you saw about conflicting types, and the differing behaviour of leepos2, comes from using the C89 rules for how function calls to undeclared functions behave.