Home > database >  What does this convoluted C type declaration mean?
What does this convoluted C type declaration mean?

Time:05-31

On a programming community I'm in, someone threw this absolute hand grenade into chat:

this is a valid function declaration in c

void* volatile* (*func(unsigned long const long volatile int, signed, long*[*][42]))(__int128* (*) (int* restrict const[static restrict const 17]));

Several of us have had a go at trying to decipher this declaration, but nobody's had any luck just yet.

CodePudding user response:

It will be more easy to understand the function declaration if to represent its declaration

void* volatile* (*func(unsigned long const long volatile int, signed, long*[*][42]))(__int128* (*) (int* restrict const[static restrict const 17]));

using typedefs.

The first one can be the following

typedef _int128* FunctionAsParameter(int* restrict const[static restrict const 17]);

The second one is the following

typedef void* volatile* FunctionAsReturnType( FunctionAsParameter * );

And at last the original function declaration will look like

FunctionAsReturnType * func( unsigned long const long volatile int, signed, long*[*][42]);

That is the return type of the function func is a pointer to function that has one parameter that in turn is pointer to function.

CodePudding user response:

This one's actually pretty easy, as the function types do not have names in their argument lists, so the only name here is the declarator func. So you just start from there and go out.

func is followed by ( so it is a function, and is preceeded by * so it returns a pointer to something. You need to find the matching ) to find the end of the argument list, which is a little tricky, but when you do, you find another argument list, so the pointer being returned is a pointer to a function, and that function returns the void * volatile * in the front of the declaration. Then you can decode the argument types of the two functions involved, but they're pretty straight-forward, other than the redundant specifiers and qualifiers you can ignore.

  • Related