What Does "Typedef Void (*Something)()" Mean

What does typedef void (*Something)() mean

It defines a pointer-to-function type. The functions return void, and the argument list is unspecified because the question is (currently, but possibly erroneously) tagged C; if it were tagged C++, then the function would take no arguments at all. To make it a function that takes no arguments (in C), you'd use:

typedef void (*MCB)(void);

This is one of the areas where there is a significant difference between C, which does not - yet - require all functions to be prototyped before being defined or used, and C++, which does.

What does “typedef void *(*Something)(unsigned int)” mean

It creates an alias some_name for a pointer to a function with a return type void* and a single unsigned int parameter. An example:

typedef void *(*my_alloc_type)(unsigned int);

void *my_alloc(unsigned int size)
{
return malloc(size);
}

int main(int argc, char *argv[])
{
my_alloc_type allocator = my_alloc;
void *p = allocator(100);
free(p);
return 0;
}

What does typedef void* key_type mean in C?

This is typically used in public header files. For the user of the API, key_type is an opaque pointer, or handle. All you can do with key_type is pass it to functions accepting a a key_type.

Those functions then internally cast that argument from key_type / void* to a pointer to a structure where their data is stored. This requires that the key_type object is previously allocated by another function from the same API.

Basically, it's a mechanism of hiding the inner workings of a library from its users, to improve modularity.

Example:

// Public api in a .h file
typedef void* xyz_key_type;
xyz_key_type xyz_init();
void xyz_print_data(xyz_key_type handle);
void xyz_close(xyz_key_type handle);

// Private implementation a the .c file
struct xyz_private_data {
int x, y, z;
};
xyz_key_type xyz_init() {
struct xyz_private_data *data = malloc(sizeof(xyz_private_data));
memset(data, 0, sizeof(*data));
return (xyz_key_type)(data);
}
void xyz_print_data(xyz_key_type handle) {
struct xyz_private_data *data = (struct xyz_private_data*)handle;
printf("x :%d, y: %d, z: %d\n", data->x, data->y, data->z);
}
void xyz_close(xyz_key_type handle) {
free(data);
}

What does this declaration typedef void foo(); mean?

It means that interrupt_handler is type synonym for function, that returns void and does not specify its parameters (so called old-style declaration). See following example, where foo_ptr is used as function pointer (this is special case where parentheses are not needed):

#include <stdio.h>

typedef void interrupt_handler();

void foo()
{
printf("foo\n");
}

int main(void)
{
void (*foo_ptr_ordinary)() = foo;
interrupt_handler *foo_ptr = foo; // no need for parantheses

foo_ptr_ordinary();
foo_ptr();

return 0;
}

What's the meaning of typedef int function(void*)?

typedef int driver_filter_t(void*);

This is a definition of a function type. It makes driver_filter_t an alias for the type that can be described as "function returning int with an argument of type pointer to void".

As for all typedefs, it creates an alias for an existing type, not a new type.

driver_filter_t is not a pointer type. You can't declare something of type driver_filter_t (the grammar doesn't allow declaring a function using a typedef name). You can declare an object that's a function pointer as, for example:

driver_filter_t *func_ptr;

Because you can't use a function type name directly without adding a * to denote a pointer type, it's probably more common to define typedefs for function pointer types, such as:

typedef int (*driver_filter_pointer)(void*);

But typedefs for function types are pefectly legal, and personally I find them clearer.

What does “typedef void (^Something)()” mean

As other answerers here say, it could be in C++/CLI.

But also, if you are on a macOS (like you hinted in one comment), this is an Objective-C block.

Its syntax is very very weird.

The block is like a C++ closures and Java anonymous inner classes, it can capture variables.

__block int insider = 0;

void(^block)() = ^{
// print insider here using your favourite method, printf for example
};

This is a complete NSObject (base Objective-C class), but is callable, this is not a mere function pointer.

Refer to this Apple document: https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/WorkingwithBlocks/WorkingwithBlocks.html

Now, we go to the important question, I want to run this on Linux, how ???

LLVM supports block syntax, but you should refer to this StackOverflow question for more: Clang block in Linux?

So, you should compile your code in the LLVM compiler, and use -fblocks and -lBlocksRuntime.

Don't forget to install those Linux packages:

llvm
clang
libblocksruntime-dev

If you are already on macOS, just use -fblocks.

typedef void (*MyCallback) : What is it and how to use it?

This particular typedef introduces a type alias named MyCallback for the type "pointer to function taking a handle, a context and a result and returning void". If you have a function taking MyCallback as a parameter, you can pass a pointer to a concrete callback as an argument:

void someFunction(MyCallback callback);
void someCallback(MyHandle handle, void* context, MyResult result, ...);

someFunction(&someCallback);
someFunction( someCallback); // the & is optional

Note that this will only work if someCallback is a top-level function or a static member function. Non-static member functions (aka methods) are completely different beasts.

In case you are simply confused by the C declarator syntax, C++11 allows the following alternative:

using MyCallback = void (*)(MyHandle handle, void* context, MyResult result,...);

typedef void(* something)(someclass* something) - what does that mean? Never seen typedef usage like this

This is a function pointer. Variables of this type point to a function whose signature is void (GpsLocation*):

void foo(GpsLocation *);

gps_location_callback f = foo;

// now use f(p) etc

Without the typedef you'd have to write:

void (*f)(GpsLocation *) = foo;

What does typedef void (*task) () do?

It's like this:

thread_pool(unsigned int num_threads, void (*tbd) ())

That is, the type is the function signature, the only "word" in which is "void." The typedef name "task" disappears in this example because we aren't using the typedef anymore.



Related Topics



Leave a reply



Submit