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

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.

Related

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

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)();

How does pointer to pointer to function (**ppf)() differ from pointer to function (*pf)() in C?

I wondering if there is a difference between int (**ppf)(int) and int (*pf)(int) in C.
C has this wierd way of treating function pointers where a function automatically transforms into a pointer to function. That allows programmers to write really wierd stuff.
double (*pf)(double) = ***&*&***&*sin;
(******&*&*puts)("Hello, world!");
This is really strange and I do not see how it is useful.
Here are my question(s) about pointer to pointer to function in C.
Does int (**ppf)(void) have more levels of indirection than int (*pf)(void)?
Is there any case where using (**ppf)() is superior to (*pf)()?
Are there any differences between them at all?
Is even it possible to get a pointer to pointer to function in C?
Yes there is a difference in between (**ppf)() and (*pf)(). And pointer to pointer to a function exist in c.
void f();
void (*pf)() = f // or &f
void (**ppf)(e) = &pf;
Any one of the following function call can be used to call function f:
using f: f(); ( &f)(); (*f)(); (**f)(); (***f)();
using pf: pf(); (*&pf)(); (*pf)(); (**pf)(); (***pf)();
using ppf: (*ppf)(); (********ppf)
They're different types. They are even different classes of types; C makes a distinction between object pointers and function pointers. Any function pointer can be assigned to any other function pointer (without a cast), which is not true for object pointers.
Obviously, pf is a function pointer. But a pointer to a function pointer is a pointer to an object, namely, to the function pointer.

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

Pointer to function that returns pointer to same function type [duplicate]

This question already has answers here:
Function Returning Itself
(10 answers)
Closed 8 years ago.
Is it possible in C to have a pointer to a function that returns a pointer to the same type of function? i.e. a pointer to a function that returns a pointer to a function that returns a pointer to a function that... etc etc.
A function pointer with int return type is:
int (*fp)(void);
So I guess a function pointer with function pointer to that would be
typedef int (*fp)(void);
fp (*pfp)(void);
But a pointer to a function that returns a function of the same type would have... an infinite recursive declaration? i.e. one would never be able to write the first typedef.
Is that correct?
I'm thinking a workaround would be to actually return a void pointer then cast that to the function pointer type maybe? e.g.
typedef void * (*fp)(void)
void * stateWork(void) {return &nextStateWork}
machine () {
fp = &stateWork;
state = (fp)(*fp)();
}
You are right in the first part, you cannot have a directly recursive type in C.
But your workaround runs afoul of UB:
data-pointers and function-pointers are fundamentally different types, roundtrip-conversion is not guaranteed:
Just use any old function-type for function pointers, they are all mutually round-trip convertible.
Aside: Making void* and function-pointers compatible is a common extension mentioned in the standard.
POSIX, Windows and MacOS do it:
J.5.7 [common extensions] Function pointer casts
1 A pointer to an object or to void may be cast to a pointer to a function, allowing data to
be invoked as a function (6.5.4).
2 A pointer to a function may be cast to a pointer to an object or to void, allowing a
function to be inspected or modified (for example, by a debugger) (6.5.4).

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