Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does ((void (*)(int))-1) mean? [duplicate]

Tags:

c

I just found this.

// /usr/include/sys/signal.h // OS X
#define SIG_ERR ((void (*)(int))-1)

What does ((void (*)(int))-1) part mean?

Is it different to

#define SIG_ERR -1

?

like image 401
Jin Kwon Avatar asked Jun 08 '15 06:06

Jin Kwon


People also ask

What is the difference between int main and void main in C?

One should stop using the ‘void main’ if doing so. int main – ‘int main’ means that our function needs to return some integer at the end of the execution and we do so by returning 0 at the end of the program. 0 is the standard for the “successful execution of the program”. main – In C89, the unspecified return type defaults to int .

Why does the operating system call the void main()?

So the operating system calls this function. When some value is returned from main (), it is returned to operating system. The void main () indicates that the main () function will not return any value, but the int main () indicates that the main () can return integer type data.

When to use the void main() instead of the exit() method?

When our program is simple, and it is not going to terminate before reaching the last line of the code, or the code is error free, then we can use the void main (). But if we want to terminate the program using exit () method, then we have to return some integer values (zero or non-zero). In that situation, the void main () will not work.

What does Foo (void) mean in C++?

However, using foo (void) restricts the function to take any argument and will throw an error. Let’s see. The above code will give us an error because we have used ‘foo (void)’ and this means we can’t pass any argument to the function ‘foo’ as we were doing in the case of ‘foo ()’.


2 Answers

This is cast to a function-pointer:

((type) value)

Where type is void (*)(int) which is pointer to function accepting one int argument and returning void, which is actually is a signature of a signal handler:

typedef void (*sighandler_t)(int);

You may decode such types with cdecl tool or web-site: http://cdecl.org/

like image 107
myaut Avatar answered Oct 05 '22 12:10

myaut


This is a cast of -1 into the function pointer which is expected as the type of SIG_ERR. Using -1 directly does not work at situations where the compiler needs the correct type.

like image 34
Rudi Avatar answered Oct 05 '22 11:10

Rudi