Is this code segment legal in C?

Function

wait () is declared in another function. Is it legal?

void panic(const int reason, const char *strg) { int ErrNo; struct machine_attributes mach; int ret, docstat, cnt; pid_t pid, wait(int *), setsid(void); ...... } 

Thanks!

+4
source share
2 answers

Yes, if this declaration matches the actual definition of the function.

 pid_t pid, wait(int *), setsid(void); 

This declares three objects: a pid_t with the name pid , a function (taking int* and returning pid_t ) with the name wait and a function (without parameters and returning pid_t ) with the name setsid .

A pid declaration is also a definition.

+6
source

Yes, it is legitimate C, and it can be useful in rare cases, for example, if you have a simple C source file (not for POSIX) that uses wait with a static link to function it and suddenly realize that you need to call POSIX wait from the function in this file. By defining the declaration in the function that calls it, you avoid contradicting the definition of static to determine the scope of static .

Note that pid_t can be obtained from other headers that do not declare wait (or any functions), but in other cases you will not be able to use a trick like this due to the lack of types.

And yes, some may call it a terrible hacking / abuse of the language. :-)

0
source

Source: https://habr.com/ru/post/1339155/


All Articles