Strange typedef to function pointer - c

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!

Related

how should i interpret this typedef declaration in c? [duplicate]

I am trying to understand what this means, the code I am looking at has
in .h
typedef void (*MCB)();
static MCB m_process;
in .C
MCB Modes::m_process = NULL;
And sometimes when I do
m_process();
I get segmentations fault, it's probably because the memory was freed, how can I debug when it gets freed?
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.
It introduces a function pointer type, pointing to a function returning nothing (void), not taking any parameters and naming the new type MCB.
The typedef defines MCB as the type of a pointer to a function that takes no arguments, and returns void.
Note that MCB Modes::m_process = NULL; is C++, not C. Also, in C, the typedef should really be typedef void (*MCB)(void);.
I'm not sure what you mean by "the memory was freed". You have a static pointer to a function; a function cannot be freed. At most, your pointer has been reset somewhere. Just debug with a memory watch on m_process.
Let's take an example
typedef void (*pt2fn)(int);
Here, we are defining a type pt2fn. Variables of this type point to functions, that take an integer as argument and does not return any value.
pt2fn kk;
Here, kk is a variable of type pt2fn, which can point to any function that takes in an integer as input and does not return any value.
Reference:https://cs.nyu.edu/courses/spring12/CSCI-GA.3033-014/Assignment1/function_pointers.html
It's a function pointer. You get a SEGMENTATION FAULT because you are trying to make a call to a function which address is invalid (NULL).
According to your specific sample, the function should return no value (void) and should receive no parameters ().
This should work:
void a()
{
printf("Hello!");
}
int main(int arcg, char** argv)
{
m_process = a;
m_process(); /* indirect call to "a" function, */
// Hello!
}
Function pointers are commonly used for some form of event handling in C. It's not its only use though...

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.

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

Typedef with function pointers in 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);

Call a void* as a function without declaring a function pointer

I've searched but couldn't find any results (my terminology may be off) so forgive me if this has been asked before.
I was wondering if there is an easy way to call a void* as a function in C without first declaring a function pointer and then assigning the function pointer the address;
ie. assuming the function to be called is type void(void)
void *ptr;
ptr = <some address>;
((void*())ptr)(); /* call ptr as function here */
with the above code, I get error C2066: cast to function type is illegal in VC2008
If this is possible, how would the syntax differ for functions with return types and multiple parameters?
Your cast should be:
((void (*)(void)) ptr)();
In general, this can be made simpler by creating a typedef for the function pointer type:
typedef void (*func_type)(void);
((func_type) ptr)();
I should, however, point out that casting an ordinary pointer (pointer to object) to or from a function pointer is not strictly legal in standard C (although it is a common extension).
I get awfully confused when casting to function types. It's easier and more readable to typedef the function pointer type:
void *ptr = ...;
typedef void (*void_f)(void);
((void_f)ptr)();
In C++: reinterpret_cast< void(*)() > (ptr) ()
The use of reinterpret_cast saves you a set of confusing parentheses, and the < > clearly sets the type apart from the call itself.

Resources