What does an asterisk in empty parentheses mean? [duplicate] - c

This question already has answers here:
How do function pointers in C work?
(12 answers)
Closed 6 years ago.
What does this c code do?
{
int (*func)();
func = (int (*)()) code;
(int)(*func)();
}
Especially I confused about subj.

It's a cast to a function pointer.
The sequence int (*)() is for a function pointer that takes an indeterminate number of arguments, and returns an int. Wrapping it in parentheses like (int (*)()), when combined with an expression, is casting the result of the expression.
The code you present, with comments:
// Declare a variable `func` which is a pointer to a function
int (*func)();
// Cast the result of the expression `code` and assign it to the variable `func`
func = (int (*)()) code;
// Use the variable `func` to call the code, cast the result to `int` (redundant)
// The returned value is also discarded
(int)(*func)();

Remember to do a typecast, we use the following:
(type_to_cast) value;
when you want to cast some value to a certain type.
Also remember you define a function pointer as
return_type (*pointer_name) (data_types_of_parameters);
And the type of a function pointer is
return_type (*) (data_types_of_parameters)
Finally, you can call a function with its pointer as
(*func_pointer)(arguments);
So, with those 4 points in mind, you see that your C code:
First defines a funciton pointer func.
Second, casts code as a function pointer and assign its value to func
Third, calls the function pointed by func, and casts the value reutrned to int.

int (*func)(); declares func as a pointer to a function that takes any number of parameters and and return int.
In statement func = (int (*)()) code;, a cast is applied to code and then assign it to the function pointer func.
(int)(*func)(); doesn't make much sense. Cast is not needed and it discards the return value. The call should be simply like
int var = func();
or
int var = (*func)();

Related

Function Pointer, what is the difference between (*f) and f [duplicate]

This question already has answers here:
How do both of these function pointer calling syntax variations work?
(4 answers)
Closed 4 years ago.
I have a an array of int and I need to assign a function pointer,
f(tab[i]) and (*f)(tab[i])
What is the difference between those 2 syntax ??
Your code looks as if you are invoking a function via a function pointer, and you're seeking to pass one element of an array to that function. The pointer might be defined as:
void (*f)(int); // Function pointer — guessed argument and return types
The difference between the invocations (*f)(tab[i]) and f(tab[i]) is spelling and history — the result is the same.
The (*f)(tab[i]) notation was mandatory before the C90 standard; the f(tab[i]) notation was allowed by C90. The advantage of (*f)(tab[i]) is that you don't accidentally head off looking for a function f — you can see it's a function pointer immediately. (Of course, that assumes you don't have a colleague who delights in writing (*sqrt)((*sin)(x)*(*cos)(x)) or other similar valid but unconscionably obscure absurdities.)
If tab is an array of integer, then this below statement
(*f)(tab[i])
is invoking a function using function pointer, where f is a function pointer which can points to a function which takes argument as tab[i]. for e.g In below code block
void print_arr(int arr_ele_value) {
printf("%d\t",arr_ele_value);
}
int main(void) {
int arr[] = {1,2,3,4,5},ele;
ele = sizeof(arr)/sizeof(arr[0]);
void (*f)(int); /* function pointer declaration */
f = print_arr; /* fun-ptr initialization */
for(int row = 0; row < ele; row++) {
(*f)(arr[row]); /* calling a function through function pointer */
}
return 0;
}
This
void (*f)(int);
is function pointer declaration which says, f is a function pointer which can points to a function which takes input argument as int and returns void.
And here
f = function;
f is assigned with function which satisfies above condition of function-pointer declaration.
And this is how you can call a function using function pointer
(*f)(arr[row]);
There is no difference nowadays for the compiler; there is only a difference for the humans.

What in the syntax rules makes prototypes of functions that return pointers and function pointers different?

The prototype for defining a function that returns a pointer looks like this:
int * function_name();
At the same time, the prototype for a function pointer looks like this:
int (*function_name)();
What syntax rules makes those two definitions different? How does precedence come into play?
For instance, is
(int *) function_name();
a prototype for a function that returns a pointer or a pointer function? (My intuition is that it defines a function that return (int *), and hence is the former.)
Edit: I realized that my question is actually about C declarations. A good resource for understanding those is http://unixwiz.net/techtips/reading-cdecl.html
What syntax rules makes those two definitions different?
The rule for parsing is:
Locate the identifier.
Always favour [] and () over *.
So, for int * function_name();, identifier function_name will goes with () instead of * and therefore function_name is declared as function that returns pointer to int.
For int (*function_name)();, The extra outer parenthesis force function_name to comes with * rather than (). So, this declares function_name as a pointer to function that returns an int.
(int *) function_name(); can't be used outside of a function. Compiler will report a syntax error. Upon seeing (int *) compiler will interpret is as a cast operator and function_name() as a function call.
Further reading: Deciphering complex declaration like void ( *( *f[] ) () ) ().
(int *) function_name();
as a function prototype is syntax error.
When calling function_name(); that way, it casts the return value to an int *:
int main(void) {
(int *) function_name(); //the return value is being casted to an `int *`
return 0;
}
The following is a function that returns a pointer:
int *function_name();
And the following is a function pointer, i.e. a pointer to a function:
int (*function_name)();
Let me cite this:
Sometimes people get confused when more stars are thrown in:
void *(*foo)(int *);
Here, the key is to read inside-out; notice that the innermost element of the expression is *foo, and that otherwise it looks like a normal function declaration. *foo should refer to a function that returns a void * and takes an int *. Consequently, foo is a pointer to just such a function.
OK, so applying the rules of reading C declarations (e.g. here):
int * function_name();
declares function_name as a function returning a pointer to int, whereas
int (* function_name)();
declares function_name as a pointer to a function returning int.

What does this weird function pointer declaration in C mean? [duplicate]

This question already has answers here:
Writing a function pointer in c
(3 answers)
How do you read C declarations?
(10 answers)
Closed 8 years ago.
Can anyone please explain what int ((*foo(int)))(int) in this does?
int (*fooptr)(int);
int ((*foo(int)))(int); // Can't understand what this does.
int main()
{
fooptr = foo(0);
fooptr(10);
}
.
int ((*foo(int)))(int);
This declares foo as a function that expects an int type argument and returns a pointer to a function that expects an int type argument and return an int.
To be more clear:
foo -- foo
foo( ) -- is a function
foo(int) -- taking an int argument
*foo(int) -- returning a pointer
(*foo(int))( ) -- to a function that
(*foo(int))(int) -- takes an int argument
int (*foo(int))(int) -- and returning an int
Here is a good explanation for the same.
foo
is what we declare.
foo(int)
It is a function that takes a single int argument
*foo(int)
and returns a pointer
((*foo(int)))(int)
to a function that takes a single int argument
int ((*foo(int)))(int)
and returns an int.
One pair of () is redundant. The same thing can be expressed as
int (*foo(int))(int)
There already answers to this, but I wanted to approach it in the opposite way.
A function declaration looks the same as a variable declaration, except that the variable name is replaced by the function name and parameters.
So this declares bar as a pointer to a function that takes an int and returns an int:
int (*bar)(int);
If, instead of a variable bar, it's a function foo(int) with that return value, you replace bar with foo(int) and get:
int (*foo(int))(int);
// ^^^^^^^^
// this was "bar" before
Add an unnecessary pair of parentheses and you get:
int ((*foo(int)))(int);
// ^ ^
// extra parentheses
According to cdecl, foo is:
declare foo as function (int) returning pointer to function (int) returning int

How to read this variable declaration?

int (*(*q)(int (*)()))();
Ok, I start with: q is a pointer to a function, which takes... Not sure what should follow next, but perhaps it's ...a pointer to function, which takes nothing and returns int, and returns pointer to a function, which takes nothing and returns int.
The trick is that q itself is a function pointer that returns and takes a function pointer. cdecl says:
declare q as pointer to function (pointer to function returning int) returning pointer to function returning int
Find the lefmost identifier, then work your way out remembering that *a[] is an array of pointer, (*a)[] is a pointer to an array, *f() is a function returning a pointer, and (*f)() is a pointer to a function. Remember that in a prototype you only need to provide the type of the parameter; int f( int ); declares f as a function that takes a single int parameter; int f( int * ); declares f as a function that takes a single int pointer as a parameter. Similarly, int f( int (*)[N] ); declares f as a function that takes a pointer to an array as a parameter. Apply these rules recursively to any parameters in the function.
Thus:
q -- q
*q -- is a pointer to
(*q)( ) -- a function
(*q)( * ) -- that takes a pointer to
(*q)( (*)()) -- a function
(*q)(int (*)()) -- returning int
*(*q)(int (*)()) -- returning a pointer to
(*(*q)(int (*)()))() -- a function
int (*(*q)(int (*)()))(); -- returning int
You are right.
q is a pointer point to a function passing an pointer to a function (passing nothing returning int) returning a pointer to a function (passing nothing returning int).
See here.
http://c-faq.com/decl/spiral.anderson.html
According to Precedence and associativity of Operators in the C Programming language.
You can understand it in the following steps:
int (*(*q)(int (*)()))();
q->*->(int (*)())->*->()->int
1 2 3 4 5
1:q is a pointer
2:q is a function pointer, the function it points to has a parameter int (*)(), which is also a function pointer, points to a function has no parameters and return type is int.
3:q is a function pointer, the function it pointers to has parameter int (*)(), which is also a function pointer, points to a function has no parameters and return type is int. And
the function which q points to has a return type : pointer.
4:q is a function pointer, the function it pointers to has parameter int (*)(), which is also a function pointer, points to a function has no parameters and return type is int. And
the function which q points to has a return type : pointer(this pointer also points to a function which has no parameter).
5:q is a function pointer, the function it pointers to has parameter int (*)(), which is also a function pointer, points to a function has no parameters and return type is int. And
the function which q points to has a return type : pointer(this pointer also points to a function which has no parameter and the function's return type is int).

What's the meaning of this declaration?

I saw this one in one of the .h files provided by my professor for a project, but I'm not too sure what this means.
int (*get_console_dev)(void);
Is it the same as
(int*) get_console_dev(void);
(a function named get_console_dev, which returns a pointer to an int?)
Thanks
It's a function pointer that can point to a function returning int and taking 0 parameters; it's not equivalent to the second line you posted.
int (*get_console_dev)(void) is a function pointer declaration. get_console_dev is a variable that can hold a reference to a function matching the specific signature.
int consoleFunction(void);
// ...
int (*get_console_dev)(void);
get_console_dev = consoleFunction;
This declaration is not the same as: (int*) get_console_dev(void);.
Check this with cdecl.
int (*get_console_dev)(void);
This is Pointer pointer function with no parameter and return type as integer.
(int*) get_console_dev(void);
This is function with no parameter and return type as pointer of type integer.
Both are completely different.
Please read up function pointers. it is a pointer to a function that takes void (as argument) and returns integer

Resources