How to understand that function / pointer declaration? [duplicate] - c

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

Related

Difference between void and non void functions [duplicate]

This question already has answers here:
Is it better to use C void arguments "void foo(void)" or not "void foo()"? [duplicate]
(6 answers)
Closed 6 months ago.
So I get that a void function won't return a value, while a int one for example will return an integer. So void main(){} doesnt return, but int main(){return 0;} will return.
My question is, what is the difference between these 3 functions. I know the first one wont return a value, the second one will return an integer. But how about the third one? I know it returns an integer because I've tested it, so what does that (void) does? Why is is there?
void main(){}
int main(){return 0;};
int main(void){return 0;};
I'm a begginer so sorry if it sounded confusing... Thanks in advance!!
(The question is about the programming language C)
Between the parenthesis are the parameters to the function.
In a function definition, the proper way to designate a function that takes no parameters is to simply specify void for the parameter list.
An empty parameter list (in a definition) is also a way of saying the function takes no parameters, however this syntax is considered deprecated and should not be used in new programs.

When is a constant pointer (not a pointer to a constant) in a function prototype required for the code to be correct? [duplicate]

This question already has answers here:
What's the point of const pointers?
(17 answers)
Closed 3 years ago.
While reviewing code, I occasionally see function prototypes with constant pointers like:
void Foo(int * const bar);
Often a programmer using such a function does not understand what he actually sees and the function is later used like this:
const int bla = 0;
Foo((int* const) &bla);
which, as far as I understand, results in undefined behavior. In this case, the constant pointer caused confusion.
My question is: When is a constant pointer (not a pointer to a constant) in a function prototype required for the code to be correct?
The const keyword is just used to avoid mistakes generating compilation errors if the variable is touched and to optimize code making the assumption that the variable does not change. It is never required. It also is of no interest at all to the caller, since C passes all parameters by value.
Both the usages of const, either const int * or int * const, are just a contract between the programmer and the compiler. They are never required.

What is the significance of asterisks postfixing a variable type in a method header? [duplicate]

This question already has answers here:
difference between int* i and int *i
(9 answers)
Why is the asterisk before the variable name, rather than after the type?
(12 answers)
Closed 3 years ago.
I have been trying to learn C as a programming language, and have been trying to solve sample problems on site like LeetCode in C programs. When I was reading over some of the skeleton code that was provided as a function header for a problem on LeetCode that I want to solve in C, the function header had asterisks post fixing some of the types, specifically like this:
int* twoSum(int* nums, int numSize, int target, int* returnSize) {
/* Code goes here */
}
After doing a fair bit of reading, I learned that prefixing a variable with an asterisk when declaring a variable reserves the variable as a pointer, but I have not been able to find anything about what it means when the type specifier itself is post fixed with an asterisk.
The spaces there don't matter.
int* nums is identical to int *nums. So are int * nums and int*nums.
All four of these declare nums as pointer to int.
It's a matter of style preference (though I wouldn't use that last one), with no effect on the generated code.

Why are these function names in parenthesis? [duplicate]

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.

What does it mean when a C function pointer has a void parameter? [duplicate]

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.

Resources