Public Group

Convert C function args to CPP function args

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

Recommended Posts

I have a bunch of code like

int dosomething(in, out)  int in, out; {


but it wont compile unless I change it to

int dosomething( int in, int out) {


is there something that can do this automatically? What would I even call the first kind to even look something up about it? Thanks

Share on other sites

Uhm, the first quote has a function declaration that is pretty weird, never seen it like that.

However, C arguments should in 99% of the time be compatible with C++. That 1% is from stuff I don't know about.

Share on other sites

"Now there's a name I haven't heard in a long time."

That type of function definition and declaration is the prototype-less version used in the original Kernighan and Ritchie C definition (it is called the K&R style).  K&R style existed before ANSI standards were defined.  While technically supported by the C standard it is marked for obsolescence.  It is strongly discouraged from use.

This method of marking functions is not supported in C++ as farmdve says.

The program 'cproto' might do what you want, but it appears to be ancient.

http://linux.die.net/man/1/cproto

Share on other sites

Oooooold school C argument declarations!  I'm not aware of any converters to handle that, but you could definitely write your own pretty easily using your method of choice.  I'd probably scan each file with some regular expressions with a bit of validation to avoid false positives, myself.

As far as what it's called, the only thing I've ever heard is "old style argument declaration".  Google search even finds examples if you use that.

Edited by Nypyren

Share on other sites
Looks like you're trying to convert K & R function declaration syntax to ANSI syntax.

I doubt you could find a tool to do it for you. You might be able to write a scrip though.

Share on other sites

Thanks guys the cproto program did exactly what I needed it converted them all and save the file!

Now to get rid of all these error: deprecated conversion from string constant to ‘char*’ lol

In my searched I found zlib uses this method I wonder how they get it to compile. Thanks again!

Share on other sites

Thanks guys the cproto program did exactly what I needed it converted them all and save the file!

Now to get rid of all these error: deprecated conversion from string constant to ‘char*’ lol

In my searched I found zlib uses this method I wonder how they get it to compile. Thanks again!

I'd suggest ignoring it, unless you're determined to modernize the code. The conversion is legal, just deprecated.

To fix it properly would require to go through every location that error pops up, and change char * to const char *, then to go through again for every new error that creates, changing variables and functions that use string literals, repeating until there are no more new errors.

Share on other sites

Fixing all the existing code might be a bit tedious, but at least all new code should be const correct right away. If the function will not modify the string behind the pointer, it should clearly say so by using const. The most critical reason is that otherwise the function will be incompatible with C++ code that uses std::string (.c_str() returns a const
char*). Personal highlight: a coworker actually adding a comment that his function will not modify the string instead of just making the darn thing const.

If the point is to C++ify the code, I'd go one step further and use std::string as much as possible (functions then take 'const string&', rather than 'const char*').

Share on other sites

For fixing the code, I'd suggest cppcheck to statically analyze the code, and it'll pop up a huge list of warnings and errors (without actually running the code), which you can take care of by doing some an hour one day, an hour the next day, etc... while your code is still compilable.

• 21
• 13
• 9
• 17
• 13