Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why don't I get a warning while passing parameter to a function which should not accept any incoming parameter? [duplicate]

I have a function like:

//in header
int Foo()
// in source file
int Foo()
{
    return 1;
}

In one the other source files I access function as:

int aninteger;
result = Foo(aninteger);

That code is compiling without giving any warning. Any idea why? How can I set visual studio to warn me about argument mismatches in calls to functions?

like image 269
CS_EE Avatar asked Dec 15 '22 03:12

CS_EE


1 Answers

This happens because, a function declared with an empty parameter list signifies that the compiler has no idea about the actual arguments to be passed or handled. So, in general, the compiler is under no obligation to produce any diagnostic.

Instead, use int Foo(void) to explicitly mention the absence of parameters.

In gcc, you can use -Wstrict-prototypes option to inform the compiler to warn you abut the former mismatch, however.

like image 164
Sourav Ghosh Avatar answered Jan 17 '23 16:01

Sourav Ghosh