How to convert a K&R function declaration to an ANSI function declaration automatically?

女生的网名这么多〃 提交于 2019-11-27 08:06:22

问题


// K&R syntax
int foo(a, p) 
int a; 
char *p; 
{ 
    return 0; 
}

// ANSI syntax
int foo(int a, char *p) 
{ 
    return 0; 
}

As you see, in K&R style, the types of variables are declared in new lines instead of in the braces. How to convert a K&R function declaration to an ANSI function declaration automatically? Does anybody know such an easy-to-use tool in Linux?


回答1:


You can use cproto or protoize (part of GCC) to generate function prototypes or convert old style (K&R) functions to ANSI format.




回答2:


Since You wanna convert a multiline string, you chould consider perl

you have

void old_style( c , a ) char c; int a; { /* some multiline code */ }

and must have

void old_style( char c, int a) {}

So

perl -i.bkp -nle 's/\((void|int|char|float|long) [a-zA-Z0-9_-]*\)([a-zA-Z0-9_-] ?,[a-zA-Z0-9_-] ?)\(.*{\)/\1(\2)/g'

or something like it, would do the trick.

It would be easier to tackle down the correct regex to this if you try it out and post in comments the output of

diff file.c file.c.bkp

for each of your source files.




回答3:


1) if you want to create standard C prototypes for a .h file use mkproto.c

mkproto thisoldfile.c > thisoldfile.h

You then could also paste over the old K&R code in the C file definition if desired.



来源:https://stackoverflow.com/questions/8068038/how-to-convert-a-kr-function-declaration-to-an-ansi-function-declaration-automa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!