Convert C function args to CPP function args

Started by
7 comments, last by Servant of the Lord 10 years, 7 months ago

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

Advertisement

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.

"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

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.

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.

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!

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.

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*').

f@dzhttp://festini.device-zero.de

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.

This topic is closed to new replies.

Advertisement