Typedef with function pointers in C? - c

I'm pretty new to C, and I'm having a really hard time reading this line of code and understanding it:
typedef void (*getnxtbyte_t)(void *stream);
From looking around, I now know that it is for a pointer pointing to a function. But could anyone help me clarify this even further? What is the name of this new type? What function is it pointing to? Is the parameter of the function (void* stream)?
Thanks in advance!

It is a tricky syntax to get used to.
What is the name of this new type?
The type is getnxtbyte_t. (You can read that trailing _t as "type". It's a popular convention.)
A variable of type getnxtbyte_t can hold the address of a function that takes one void * parameter and has return type void.
What function is it pointing to?
Wrong question.
That code merely defines the type. No variables are created so there's no "it" to point to anything.
If you know of a function with the correct signature, such as:
void some_func(void*) {}
You may now create a pointer to it using that typedef:
getnxtbyte_t my_function_pointer = some_func;

This typedef creates a type called getnxtbyte_t. That type is for a pointer to a function that returns void (i.e. nothing), as shown in the second word. That function takes a single parameter, which is a void *, shown by stream.
So if you had a function with a declaration like this:
void some_function(void *any_name);
Then you could use a typedef like the one in your post:
void *some_param = NULL;
typedef void (*getnxtbyte_t)(void *stream); // declare typedef
getnxtbyte_t func = some_function; // assign
func(some_param); // call

The function pointer type name is getnxtbyte_t. It's not pointing to anything now -- this is a type of pointer, not an actual pointer. It's just like saying
typedef struct foo {int x;} Foo;
you define a type Foo, but no actual instance of that type. And finally, yes, the function takes a single void* argument, and returns void.

I am also new to C, so if there are any errors please correct me.
A pointer that points to a function is formatted like so:
datatype (*POINTER_NAME)(PARAMETERS);
So that's the data type the pointed function returns, the name of the pointer and the parameters the pointed function takes.
Here's how a function pointer looks compared to a normal function declaration:
// normal function declaration
void getnxtbyte_t(void *stream);
// function pointer
void (*getnxtbyte_t)(void *stream);
typedef allows us to create our own type.
// will create a type called getnxtbyte_t
typedef void (*getnxtbyte_t)(void *stream);
At this point we have only declared a type; we are not pointing to anything. So let's create a pointer named func_ptr and point it to a function.
// func_ptr is a pointer of type getnxtbyte_t
getnxtbyte_t func_ptr = another_function;
// calling func_ptr is now the same as calling another_function
func_ptr(an_argument);
// had we not used typedef, we would type:
void (*getnxtbyte_t)(void *stream) = another_func;
getnxtbyte_t(an_argument);

Related

Is an asterisk optional in a function pointer?

The code I am using has this statement:
typedef void ( udp_data_notify )(OS_FIFO * pfifo, WORD port);
This looks like a declaration of a function pointer for udp_data_notify, however there is no *. Can it still be a function pointer without an asterisk?
Here is a statement that uses udp_data_notify:
void RegisterUDPFifoWithNotify( WORD dp, OS_FIFO *pnewfifo , udp_data_notify * nudp)
Any help as to what is happening would be appreciated!
A typedef such as:
typedef void name(int);
(the parenthesis around name are redundant) will define name as the type of a function, not a function pointer. That would be:
typedef void (*pname)(int);
You are probably wondering what they are good for. Well, function types are not very useful, other than for declaring pointers:
name *pointer_to_function;
And that can be made arguably more readable with the other typedef:
pname pointer_to_function;
That's because you cannot define a variable of type function. If you try, you will simply write the prototype of a function, but in a quite obfuscated syntax:
name foo; //declaration (prototype), not variable
void foo(int x) //definition
{
}
But note that you cannot use the typedef to define the function:
name foo {} //syntax error!
As already said, the typedef declares an alias for function type. When you want to use it to declare a function pointer, an asterisk is required (udp_data_notify* x). A declaration without the asterisk (udp_data_notify x) would be a function declaration, except in one special case. When used in a parameter a function type is automatically turned into the corresponding function pointer type:
typedef void F(void);
void foo(F a, F* b) // special case; a and b are both function pointers
{
F c; // function declaration
F* d; // function pointer
}
The typedef you show declares udp_data_notify to be an alias for a function type. Then the parameter declaration udp_data_notify *nudp declares nudp to be a pointer to a function of that type. There is no function call here.
Regarding the function-call operator (postfix ()), the expression that designates the called function must be a pointer to a function. When you call a function normally, without a pointer, such as sqrt(2), the function is automatically converted to a pointer for you. So sqrt(2) is actually (&sqrt)(2). When you call a function with a pointer, the call is in the right form already.

Strange typedef to function pointer

I am using a code written by somebody else, where they intend to use a function pointer. They do a very strange typdef that I can not understand. Below the code
typedef void (myType)(void);
typedef myType *myTypePtr;
I can understand that the main idea with myTypePtr is to create a "pointer to a function that receives void and returns void. But what about the original myType? What is that? a function type? Is not clear to me.
Furthermore, later there is this function prototype
int createData(int id,int *initInfo, myTypePtr startAddress)
However I get the compile error "expected declaration specifiers or '...' before 'myTypePtr' any idea why this is happening?. Thank you very much.
This first typedef
typedef void (myType)(void);
provides myType as a synonym for the type void (void), the type of a function that takes no arguments and returns void. The parentheses around myType aren't actually necessary here; you could also write
typedef void myType(void);
to make it clearer that it's the type of a function that takes void and returns void. Note that you can't actually declare any variables of function type; the only way to get an object of function type in C is to define an actual function.
The second typedef
typedef myType *myTypePtr;
then says that myTypePtr has a type that's equal to a pointer to a myType, which means that it's a pointer to a function that takes no arguments and returns void. This new type is equivalent to the type void (*)(void), but is done a bit indirectly.
As for your second error, I can't say for certain what's up without more context. Please post a minimal test case so that we can see what's causing the error.
Hope this helps!

Struct Notation

I need clarification for the following notation in C:
I have a struct, and within that struct I have the following field:
bool (* process_builtin)(struct esh_command *);
I am pretty confused here.. So this is a boolean field.. What exactly is process_builtin? I already have a struct esh_command defined, but I have no clue where this plays in this field. Can someone please explain the whole thing's meaning?
That's not a boolean field, that's a pointer to a function taking a struct esh_command* and returning a bool; the field is called process_builtin.
You could also write:
typedef bool (* process_builtin_t)(struct esh_command *);
in which case process_builtin_t would be a type and in which case you could write the definition of that struct member as:
process_builtin_t process_builtin;
This a pointer to function. Using cdecl (and changing bool to int) reveals:
declare process_builtin as pointer to function (pointer to struct esh_command) returning int
This link is a permalink to the entire output.
process_builtin is a function pointer. The function it points to takes a esh_command* as parameter and returns a bool.
That is a pointer to a function that takes a pointer to struct esh_command as an argument and returns a bool value.
http://www.cprogramming.com/tutorial/function-pointers.html

How to declare function pointer in header and c-file?

I'm a little confused over how to declare a function pointer in a header file.
I want to use it in main and a file called menus.c and declare it in menus.h I assume.
We want to initialize to point to a certain function.
it looks like this:
void (*current_menu)(int);
What do we write in menus.c, menus.h and main?
A function pointer is still a pointer, meaning it's still a variable.
If you want a variable to be visible from several source files, the simplest solution is to declare it extern in a header, with the definition elsewhere.
In a header:
extern void (*current_menu)(int);
In one source file:
void (*current_menu)(int) = &the_func_i_want;
It's often helpful to use typedef with function pointers, so you can name the type to something descriptive:
typedef void (*MenuFunction)(int);
Then you would have a global variable of this type, probably in menus.c, and declared (with extern) in menus.h:
static void my_first_menu_function(int x)
{
printf("the menu function got %d\n", x);
}
MenuFunction current_menu = my_first_menu_function;
From main.c, you can then do:
#include "menu.h"
current_menu(4711);
to call whatever function is currently pointed at by current_menu.
A pointer function itself does not have a function definition. It's nothing more than a pointer to a type, the type being specified by the return type of the function and the parameter list. What you need to do is define a function with the same parameter list and return type, then use your pointer function to hold that function's address. You can then call the function through the pointer.

call Struct member in C

I have a question regarding C, would appreciate those who are willing to share their knowledge.
While I was reading a code, I got stumbbled in a struct that its member is called in a way that I have never seen before.
The code basically is below :
Code to call the struct member
struct struct_name gzw;
gzw.cb = otherfunct;
where the struct is defined below
struct struct_name {
int bela;
unsigned int packet;
int (*cb)(struct struct_name *fd, unsigned int packet2);
};
I kinda confused, because as I know, the cb member should be a pointer, with two parameter isn't it? howcome struct_name can call "cb" , and not (*cb with 2 parameters) ?
Thank you for your kindness response
cb is a function pointer. You can assign it to point at any function whose prototype (i.e. argument number, types and return type) matches that of the function-pointer type.
You can then call that function via the function pointer, as:
gzw.cb(arg1, arg2);
the CB member is a function pointer that takes two parameters and returns and int. The call you are confused about is assigning a pointer value and therefore does not need to reference the parameters. to the call the function the parameters would be used gzw.cb(p1,p2).
This is a function pointer. Basically, you assign a function to the struct like you would assign any other value.
Yes, cb is a function pointer that takes two arguments and returns an int.
It is not correct to say "struct_name calls cb" Instead, the structure contains a function pointer which you can call with gzw.cb(arg1, arg2);.
yes you are right. the member variable cb is a function pointer variable, taking a struct struct_name* and an integer as input and returns an int.
To call the function you have to do something like this:
int ret = gzw.cb(&gzw, 10);

Resources