This question already has answers here:
How do function pointers in C work?
(12 answers)
C function pointer syntax
(4 answers)
Closed 5 years ago.
I've been meaning to ask this question for a while now. What's going on with these functions? Why are the names in parenthesis?
void (*think)(gentity_t *self);
void (*reached)(gentity_t *self); // movers call this when hitting endpoint
void (*blocked)(gentity_t *self, gentity_t *other);
void (*touch)(gentity_t *self, gentity_t *other, trace_t *trace);
In your examples, the parenthesis in function name means that variable of pointing the function address.
If you don't use the parenthesis
void * think(gentity_t *self);// equal (void *) think(gentity_t *self);
It means the definition of a function with name:think, return: void *, parameter: gentity_t *self;
These are the variable of the pointing the functions.
These declarations are function pointers, which point to a function and can be changed at any time.
I suggest you do some research on function pointers in C because they are very useful.
If you know C++'s std::function then these are effectively the old C version of them.
These are function pointers and not the function names. So they can point to any function of same type and properties.
Related
This question already has answers here:
How do function pointers in C work?
(12 answers)
Closed 10 months ago.
In C, one can declare pointers to functions like that:
void (*Func1)(int)
I believe that I've understood what this means (in this case, a pointer to a function which returns void and takes an int as parameter) and how to declare and use such pointers.
However, I now have come across declarations like the following:
void (*Func2(int, int))(int)
I am struggling with understanding this syntax. What exactly is declared here? Probably it is a pointer to a function, but I always thought that the closing round parenthesis after the pointer's name is necessary then, so I am completely unsure now.
Could anybody explain, step by step, what the above declaration means?
From https://cdecl.org/?q=void+%28*Func2%28int%2C+int%29%29%28int%29
void (*Func2(int, int))(int)
declare Func2 as function (int, int) returning pointer to function (int) returning void
This question already has answers here:
Alternative (K&R) C syntax for function declaration versus prototypes
(5 answers)
Closed 3 years ago.
I stumbled upon these lines when looking into putchar.c
I'm wondering about why the arguments ptr and c are declared outside the arguments body ?
Is this some kind of "good old way" or does it have some actual use ?
int
_putchar_r (ptr, c)
struct _reent *ptr;
int c;
{
return __sputc (c, _stdout_r (ptr));
}
Indeed it is the "olden" way of declaring a function's parameters.
I kind of like it, because it serves as a constant reminder that all a function's parameters are local variables that exist only in the scope of the function and that any argument passed into any function ALWAYS is a value copy.
Its a K&R C style introduced in classic C Programming Book
It is a function definition with an identifier list. Each identifier in the identifier list is declared before the compound statement of the function.
So a function can be defined either with a parameter type list or using the old style with an identifier list.
This question already has answers here:
What does a typedef with parenthesis like "typedef int (f)(void)" mean? Is it a function prototype?
(6 answers)
Understanding typedefs for function pointers in C
(8 answers)
Is it a good idea to typedef pointers?
(15 answers)
Closed 3 years ago.
I'm studying in-depths of languages, so I can understand what's happening in code, rather than print some things and watch and see what happens.
Recently, in search for better implementation for class function table I found myself stumbled upon this C language standard: http://www.iso-9899.info/wiki/Typedef_Function_Type
I've tried it out and it seems working feature:
typedef void fnr(int x);
main()
{
fnr t;
}
This seemed a glorious day for me searching the way to pack up functions into my structure, until I realized, that fnr t; is not as useful as I had intended. It can neither be assigned, nor used the proper way I wished it to be (probably lambda for C-users). It does not even exist in disassembly!
What does this language feature do? What can it be used for besides simplifying function pointers?
You've declared fnr as a function type. While a function type cannot be assigned to, a pointer to a function type can. For example:
typedef void fnr(int x);
void f(int x)
{
printf("x=%d\n", x);
}
int main()
{
fnr *t = f;
t(1);
}
You could also define the typedef as a function pointer:
typedef void (*fnr)(int x);
...
fnr t = f;
Using a typedef for a function pointer is most useful when a function pointer is either passed to or returned from a function. As an example, let's look at the signal function which does both:
typedef void (*sighandler_t)(int);
sighandler_t signal(int signum, sighandler_t handler);
The second parameter to this function is a pointer to a signal handling function, and it also returns a pointer to a signal handling function. Without the typedef, it would look like this:
void (*signal(int signum, void (*handler)(int)))(int)
The typedef facility acts primarily as an abstraction mechanism - it's used to create an alias for a type name that either hides or simplifies implementation details. The FILE type in stdio.h is probably the canonical example in C - FILE is a typedef name for some other type (often a struct type) that stores state information for a stream. We don't mess with a FILE object's contents directly - instead, we create a pointer to a FILE that's operated on by the various C I/O routines (fopen, fclose, fread, fwrite, fscanf, fprintf, etc.).
EDIT
As for typedef-ing function types...
You'll often see it used to simplify a callback declaration:
typedef void (*callback)( /* callback arguments */ );
...
void do_something( T arg1, T arg2, callback c );
callback is an alias for the type "pointer to function taking some callback arguments and returning void".
This question already has answers here:
How do function pointers in C work?
(12 answers)
Closed 4 years ago.
I am not familiar with the syntax below for the following struct
struct fp {
int (*fp)();
}
What is int (*fp)() ? I understand that it is an integer and *fp is a pointer but I do not understand what that parentheses in (*fp)() do.
fp is a pointer to a function with empty parameters list.
I.e.
int myfunc() //define function
{
return 0;
}
struct fp //define structure
{
int (*fp)();
} mystruct;
mystruct.fp = &myfunc; //assign function pointer to structure element
int a = mystruct.fp(); //Call function through pointer
There are many ways to read C declarations, that can be very complicate in some cases. Start reading this https://parrt.cs.usfca.edu/doc/how-to-read-C-declarations.html.
You can google for "how to read c declarations" for more in depth explanation and further tips.
As Swordfish pointed out, the use of empty parameter list imply other sensible points about function definitions, that could be worth to deep. Please refer to Swordfish's comment below for the main points relative to functions definition.
I will refer only to the §6.11.6 Function declarators (of §6.11 Future language directions):
The use of function declarators with empty parentheses (not
prototype-format parameter type declarators) is an obsolescent
feature.
It is a function pointer. They are powerful and quite hard to get your head around if you are a beginner.
It is a strcture that wraps a function-pointer.
Have a look here: How do function pointers in C work?
It is a declaration of the variable fp which is a pointer to a function that returns an int and takes an unspecified list of arguments.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C void arguments
I am looking at some OpenGL graphics code and it has the following:
glutIdleFunc(void(*func)(void));
When does it mean to have a function pointer with a void argument in C? Does this mean the function can take in any arguments or is not allow to take in any arguments, or something else?
It means you have to pass a pointer to a function that has no parameters and returns nothing.
void func(void)
is a function that takes no parameters and does not return anything.
This is not to be confused with:
void func()
which in C (not C++) is a function that has no parameter checking, and does not return anything.
This is not to be confused with:
func(void)
which is a function which takes no parameters and returns an int, by default.