typeof is available in C23 as well as GCC and Clang as extensions (might need __typeof__ depending on flags)
See:
I'm not sure how to organize this into the existing examples though. Here's my attempt at using typeof on everything in the C section, with original as comparison. Some of these aren't really better.
Note that I haven't tested these.
As a variable:
returnType (*variableName)(parameterTypes) = function_name;
typeof(returnType (parameterTypes)) *variableName = function_name;
As a static const variable:
static returnType (* const variableName)(parameterTypes) = function_name;
static typeof(returnType (parameterTypes)) * const variableName = function_name;
As an array:
returnType (*arrayName[])(parameterTypes) = {function_name0, ...};
typeof(returnType (parameterTypes)) *arrayName[] = {function_name0, ...};
As a parameter to a function:
int my_function(returnType (*parameterName)(parameterTypes));
int my_function(typeof(returnType (parameterTypes)) *parameterName);
As a return value from a function:
returnType (*my_function(int, ...))(parameterTypes);
typeof(returnType (parameterTypes)) *my_function(int, ...);
As a cast (but try not to cast functions):
... (returnType (*)(parameterTypes))my_expression ...
... (typeof(returnType (parameterTypes)) *)my_expression ...
As a function pointer typedef:
typedef returnType (*typeName)(parameterTypes);
typedef typeof(returnType (parameterTypes)) *typeName;
As a function typedef:
typedef returnType typeName(parameterTypes);
typedef typeof(returnType (parameterTypes)) typeName;
typeofis available in C23 as well as GCC and Clang as extensions (might need__typeof__depending on flags)See:
I'm not sure how to organize this into the existing examples though. Here's my attempt at using
typeofon everything in the C section, with original as comparison. Some of these aren't really better.Note that I haven't tested these.
As a variable:
As a static const variable:
As an array:
As a parameter to a function:
As a return value from a function:
As a cast (but try not to cast functions):
As a function pointer typedef:
As a function typedef: