pan8863
pan8863

Reputation: 733

typedef in C, what does this code mean

I started my programming in Java, and I'm new to C/C++, so this is new for me.

In one of my coding assignments I came across the following syntax:

typedef void function_name (void *param);

I looked up what typedef does, and I know what it does by definition. But not able to figure out what above statement means.

Next, this is the code that confused me more.

static function_name some_func;

Can someone throw any light on this?

Upvotes: 1

Views: 206

Answers (2)

Joshua Rahm
Joshua Rahm

Reputation: 574

Just to make sure we are on the same page. A typedef is a way of creating a new type that actually refers to an old type. For example the line

typedef int time_t;

creates a new type time_t that is just an alias for the type int. This is quite useful for application development because it allows us to encapsulate our types. For example, lets assume now, after 30 years, we realize that the 32-bit int type is no longer large enough to store a time value. With a good architecture, we can change that line to be:

typedef long long int time_t;

And in theory everything will be fine and dandy!

Back to the original question. The line you see is a typedef similar to this, but with some strange syntax. This strange syntax is a way of typedef'ing a function pointer -- what is a function pointer? A function pointer is a way of passing a function around as a value which allows the code to call a function without necessarily knowing exactly what function it is.

Back to the typedef. It is saying "create a new type function_name such that it is the class of functions that accept a void* as input and return void". So the new type function_name is the type given to any function that meet those requirements (return nothing, takes void*). It lets me write code like this:

typedef void function_name (void* data);

void my_function(void* data) // my_function has type function_name
{
     printf("%p\n", data);
}

void invoke_twice(function_name fn, void* data)
{
     fn(data); fn(data);
}

int main(int argc, char** argv)
{
     invoke_twice(my_function, NULL); // my function _is_ a function
     // should print
     // 0x0
     // 0x0
}

The second statement

static function_name some_func;

Is a little puzzling to me why anyone would do this. It is a very obfuscated way of forward-declaring a function. In other words this statement is equivalent to

void some_func(void* data);

But who am i to judge other code. Without the full context it's hard to extrapolate the intent behind such design decisions.

Upvotes: 1

wallyk
wallyk

Reputation: 57774

These two declarations

typedef void function_name (void *param);

static function_name some_func;

are equivalent to

 static void some_func (void *param);

For declaring a single function, that is a pretty long winded approach, but the typedef would simplify declaring a bunch of functions of the same shape:

static function_name func1;
static function_name func2;
static function_name func3;
static function_name func4;

Whether this is a net gain in clarity of declarations is unclear. But a more complicated function declaration could easily be helpful:

 typedef struct some_st **(*func) (int v, char **a, int (*kw) (int p1, int p2));

This declares func as a pointer to a function which returns an indirect pointer to a struct and the function takes 3 parameters, the last of which is a function pointer.

Upvotes: 4

Related Questions