Jump to content
  • Advertisement
Sign in to follow this  
Endar

warning on an empty parameter list in function declaration

This topic is 3795 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm looking to find if there's a way to raise a warning when a function is declared, in C, with an empty parameter list. I'm trying to detect when I don't put a 'void' in the function declaration of a function that has an empty parameter list. Is this possible?

Share this post


Link to post
Share on other sites
Advertisement
If -Wall -ansi -pedantic doesn't catch it, then there isn't a whole lot else you can do. I do wonder why you would care in particular - i don't recall empty parameter lists being exploited very often.

Share this post


Link to post
Share on other sites
Good programming practice. One of the more senior programmers at work mentioned it and I just wondered if it were possible.

Share this post


Link to post
Share on other sites
What exactly is the keyword void in a parameter list buying you? Is it not more intuitive when there's no parameters for the parameter list to actually be empty? I never understood that.

I find the practice of putting void in the parameter list equatable to keeping $0 bills in your wallet on days when you're not shopping.

It's not good programming practice. At best, it's stylistic.

Share this post


Link to post
Share on other sites
Quote:
Original post by Rydinare
What exactly is the keyword void in a parameter list buying you? Is it not more intuitive when there's no parameters for the parameter list to actually be empty? I never understood that.


In standard C (as opposed to C++), no parameters to a function actually indicates an arbitrary number of arguments, IIRC. How one is supposed to access those arguments I never determined...

Share this post


Link to post
Share on other sites
Quote:
Original post by swiftcoder
Quote:
Original post by Rydinare
What exactly is the keyword void in a parameter list buying you? Is it not more intuitive when there's no parameters for the parameter list to actually be empty? I never understood that.


In standard C (as opposed to C++), no parameters to a function actually indicates an arbitrary number of arguments, IIRC. How one is supposed to access those arguments I never determined...


Ahh. I didn't realize that C treated it differently. That explains where the usage of foo(void) came from. Even so, that's very peculiar that they would do something so silly, when there's no way to access them (or is there?).

Just another reason why C is quite disappointing as a language.

Share this post


Link to post
Share on other sites
Quote:
Original post by Rydinare
Ahh. I didn't realize that C treated it differently. That explains where the usage of foo(void) came from. Even so, that's very peculiar that they would do something so silly, when there's no way to access them (or is there?).

Just another reason why C is quite disappointing as a language.


C++ is no better really. You get similar behavior with throw specifications (a function that does not specify any thrown exceptions can throw any, you have to explicitly specify that a function will throw no excpetions).

Share this post


Link to post
Share on other sites
Quote:
Original post by Rydinare
Quote:
Original post by swiftcoder
Quote:
Original post by Rydinare
What exactly is the keyword void in a parameter list buying you? Is it not more intuitive when there's no parameters for the parameter list to actually be empty? I never understood that.


In standard C (as opposed to C++), no parameters to a function actually indicates an arbitrary number of arguments, IIRC. How one is supposed to access those arguments I never determined...


Ahh. I didn't realize that C treated it differently. That explains where the usage of foo(void) came from. Even so, that's very peculiar that they would do something so silly, when there's no way to access them (or is there?).

Just another reason why C is quite disappointing as a language.


C was designed as a light layer on top of assembly which it does a great job of accomplishing.

Share this post


Link to post
Share on other sites
Quote:
Original post by Rydinare
Ahh. I didn't realize that C treated it differently. That explains where the usage of foo(void) came from. Even so, that's very peculiar that they would do something so silly, when there's no way to access them (or is there?).

In C function declarations don't necessarily match function definitions 100%. You can declare a function

int foo();

and later define it as

int foo(int bar);

and then hope and pray that whenever foo() is called, they use a single function parameter. In more practical terms, this can be used to bind functions with different parameter types into function pointers. Common examples in lex/yacc bind functions like sqrt() and pow() (which take different number of arguments) into the same function pointer type and then use parse trees to determine how many arguments to pass to the function pointers.

Share this post


Link to post
Share on other sites
Quote:
Original post by swiftcoder
Quote:
Original post by Rydinare
What exactly is the keyword void in a parameter list buying you? Is it not more intuitive when there's no parameters for the parameter list to actually be empty? I never understood that.


In standard C (as opposed to C++), no parameters to a function actually indicates an arbitrary number of arguments, IIRC. How one is supposed to access those arguments I never determined...


Not 100% sure but probably the same way the ... access variables.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!