fluffy
fluffy

Reputation: 5314

What does this convoluted C type declaration mean?

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.

Upvotes: 2

Views: 136

Answers (3)

chqrlie
chqrlie

Reputation: 144770

Using the spiral rule:

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

func is a function taking a volatile const unsigned long long, an int and a pointer to the initial element of a variable length array of arrays of 42 pointers to long, returning a pointer to a function F1 taking a pointer to a function F2 taking a const pointer to the initial element of an array of at least 17 constant pointers to int, returning a pointer to an __int128, itself (F1) returning a volatile pointer to a void *... Or something close to that.

  • The restrict keywords are here for information only in a function prototype.
  • qualifying a function argument as volatile and or const is unusual and probably useless in a function prototype.

Upvotes: 2

Chris Dodd
Chris Dodd

Reputation: 126223

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.

Upvotes: 1

Vlad from Moscow
Vlad from Moscow

Reputation: 310990

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.

Upvotes: 4

Related Questions