derefferencing a function pointer with and without parentheses [duplicate] - c

This question already has answers here:
How does dereferencing of a function pointer happen?
(5 answers)
Closed 8 years ago.
What is the difference in dereferencing a function pointer with and without parentheses.
both the mechanism are working same on linux Gnu Gcc.
void (*fp)(void); //function pointer
void func(void);
fp = func;
(*fp)(); //dereferencing with parentheses
fp(); // without parentheses

You need to know that
(*fp)() = fp()
The function pointer can be called in both the ways and both are valid and same.
Check the below link:
How does dereferencing of a function pointer happen?

Related

What does caret (^) in a C function declaration mean? [duplicate]

This question already has answers here:
Caret character between types rather than variables, surrounded by parentheses
(2 answers)
C "block" caret character
(2 answers)
Caret in objective C
(3 answers)
What does this caret ^ syntax, with void on either side mean? [duplicate]
(4 answers)
Closed 2 years ago.
In the macos documentation for qsort, we see the declaration:
void qsort_b(void *base, size_t nel, size_t width, int (^compar)(const void *, const void *));
What is the meaning of the caret?
The ^ in the function declaration indicates that the callback is a block pointer rather than a function pointer.
As stated later in the man page:
The heapsort_b(), mergesort_b(), and qsort_b() routines are like the corresponding routines without the _b suffix, expect that the compar
callback is a block pointer instead of a function pointer.

Passing address of an integer variable to pthread_exit [duplicate]

This question already has answers here:
Argument conversion: (normal) pointer to void pointer, cast needed?
(1 answer)
Concept of void pointer in C programming
(16 answers)
Closed 2 years ago.
The pthread_exit function should take a void pointer as input. I'm wondering how come it's possible to pass the address of an integer variable (e.g. pthread_exit(&ret1) here) without performing a cast conversion.

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.

Do I understand correctly the C syntax? [duplicate]

This question already has answers here:
How do you read C declarations?
(10 answers)
Closed 4 years ago.
I have this syntax:
double (*(f(double (*)(int))))(int);
Do I understand correctly syntax above that f is function that gets pointer to function that receive int and return a double?
No; it's actually even more complicated than that.
cdecl.org glosses this type definition as:
declare f as function (pointer to function (int) returning double) returning pointer to function (int) returning double
In other words, the function pointed to by f takes a function pointer as an argument, and returns a function pointer. Both of those function pointers must be to functions which take an int as an argument and return a double.
You could simplify this definition a bit using an intermediate typedef as follows:
typedef double (*int_to_double_function)(int);
int_to_double_function (*f)(int_to_double_function);

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).

Resources