What are the benefits of function pointers in C? [closed] - c

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I want to know the benefits of function pointers in computer and embedded systems programming.

Same as regular typdefs, incredibly useful for 2 and 4. Instead of typing the whole thing, you can now use myFuncDef
A function declaration for a function that recieves a function pointer. A function that takes a function pointer of 1. would thus look int add2to3(myFuncDef functionPtr);
-
A function that takes an int and (probably based on that) returns you a function pointer. Using the typedef it would have looked like myFuncDef functionFactory(int n);
Without typedefs these chains of pointers to pointers could get incredibly long as can be seen in this question: C syntax for functions returning function pointers

1) typdef of function pointer will help to return a function pointer from a function.
i.e., typedef int (*myFuncDef)(int, int);
myDuncDef retFunc() here retFunc return the function pointer.
2)int add2to3(int (*functionPtr)(int, int)); here add2to3function gets the function pointer with specified type as a argument.
3) ------
4) int (*functionFactory(int n))(int, int) this is another way of returning a function pointer.

typedef(s) are used to simplify declarations.
This declaration
int add2to3(int (*functionPtr)(int, int));
declares a function that accepts as argument a pointer to a function with two parameters of the type int and has the return type int.
To make this declaration simpler you can write the parameter as having a type of function instead of the type pointer to function.
int add2to3( int function( int, int ) );
Or you can use the typedef
typedef int (*myFuncDef)(int, int);
//...
int add2to3( myFuncDef functionPtr );
This declaration
int (*functionFactory(int n))(int, int);
declares a function that has one parameter of the type int and has the return type pointer to a function that has two parameters of the type int and has the return type int.
Again using the typedef above you can simplify the declaration
typedef int (*myFuncDef)(int, int);
//...
myFuncDef functionFactory(int n);

Related

Typecasting void double pointer to void, why? [duplicate]

This question already has answers here:
What does casting to `void` really do? [duplicate]
(4 answers)
Closed 3 years ago.
The code I'm working on has a function which has a double pointer of type void, in the function pointer the double pointer is typecasted to void, and the pointer is not used anywhere else. I cant find any where why this is done. someone please shed some light.
static void kbd_callback(const char *name, int name_len,
const char *instruction, int instruction_len,
int num_prompts,
const LIBSSH2_USERAUTH_KBDINT_PROMPT *prompts,
LIBSSH2_USERAUTH_KBDINT_RESPONSE *responses,
void **abstract /* <-- This */)
{
int i;
size_t n;
char buf[1024];
(void)abstract; // <---- and this
...
}
The type of the callback is probably part of the API of a LIBSSH2 library. The library passes every parameter to the callback that it expects the callback will need. Whatever that parameter is, this particular callback doesn't need it. The programmer has four choices:
He can leave out the name of the parameter in the prototype, replacing void **abstract with void **. This makes it so that someone trying to understand his code has to look at the LIBSSH2 API to understand what the last parameter is.
He can just not use the parameter. But this will get him a warning from his compiler.
He can use the parameter in a way that has no consequence to hide the warning.
He could comment out the parameter name, like this: void ** /*abstract*/.
This programmer choose option 3.
Personally, I tend to prefer option 4 for this case. I'd also prefer to see something like this:
#define OK_UNUSED(a) if (false && (a)); else (void) 0
...
OK_UNUSED(abstract);
This makes it very clear that it's okay that the parameter is unused.
It's a double pointer meaning it is a pointer to a pointer. Being of type void it can hold any type. So i'm assuming it will be used to hold a pointer, in this case i'm assuming the developer does not know what type of pointer it will be passed or it holds different types already declared and initialized in the program.

Is there a purpose of typedefing a function in C, aside from use with a function pointer?

The following code is valid C:
typedef int math_op(int, int); // Function type definition
The only time I've ever seen a function type declared and then used is in the case where we make a statement like the following:
math_op *mOp = add;
where add may be:
int add(int a, int b)
{
return a + b;
}
The following compiles as well given the initial declaration:
math_op mOp;
However, here it is not declared as a function pointer, but as a function itself. However, I know no actual usage for this and find it to be quite confusing because it seems we're declaring a function with no actual definition.
My question is, is there any other usage for a typedef'd function aside from using it as a function pointer type like this? Note that I am already aware of the alternative function pointer type syntax typedef int (*math_op)(int,int); which creates a function pointer type, whereas my original syntax above creates a function type to which we later point to.
According to clang:
error: non-object type 'math_op' (aka 'int (int, int)') is not
assignable: math_op tee = 0;
So that really limits our options.
The only use I can find is if you want to forward declare a bunch of functions with the same type signature. This is probably not a good idea and would probably increase the WTF's / line of your code.
math_op tee;
int tee (int x, int y) {
return x + y;
}

Function Pointers Using typedef in C [duplicate]

This question already has answers here:
C function pointer syntax
(4 answers)
typedef a function pointer type
(2 answers)
Closed 5 years ago.
If I wanted to declare a typedef for a certain type, I would go for a syntax like this one, in this example:
typedef int INT
But when I wanted to create a typedef for a function, I was expecting the following syntax to be the one:
typedef void (*)(int, char) myfunc;
Instead the correct one is:
typedef void (*myfunc)(int, char);
So why the first one is not correct?
A typedef looks exactly like a "normal" declaration. If you declare a function pointer, it would look like this:
void (*myfunc)(int, char);
So, the typedef looks the same, with the only difference that the declared identifier doesn't refer to an object of the type, but to a type alias name instead:
typedef void (*myfunc)(int, char);
As for the "why?" -- Well, because the language is designed that way, but I guess once you understand how it works, it's arguably the easiest way not to introduce a different syntax for typedef declarations. This design is following the principle of least surprise, treating typedef declarations somehow differently would be needlessly complicated.
The basic way to create function pointer is void (*myfunc)(int, char);
So to create a new type that is a function pointer which returns void and get int and char as argument is
typedef void (*myfunc)(int, char);
that is why the first one is incorrect
There is no need of adding (*) before function pointer.
But when I wanted to create a typedef for a function, I was expecting
the following syntax to be the one:
typedef void (*)(int, char) myfunc;
You can of course write you own compiler of your own language.
But C syntax is like this.
Here is the another example:
typedef char foo[10];

What is the use of this kind of declaration? [duplicate]

This question already has answers here:
typedef int (*pf) needs explaining
(5 answers)
Closed 8 years ago.
I came across this kind of a declaration.
typedef int (*func) (int,int,int);
What is the meaning and use of this?
It defines func as type for function which accepts 3 integers and returns integer.
This is helpful when you pass functions as callbacks or put function addresses into arrays or something like that.
That's the typedef'd name. It reads as: func is a pointer to a function that takes three ints and returns an int.
You can see more on this link
It defines a type func which is a pointer to a function returning an int and taking 3 int arguments.
An example of using this would be:
typedef int (*func) (int, int, int);
int foo(int a, int b, int c) {
return a + b * c;
}
...
// Declare a variable of type func and make it point to foo.
// Note that the "address of" operator (&) can be omitted when taking the
// address of a function.
func f = foo;
// This will call foo with the arguments 2, 3, 4
f(2, 3, 4);
A more realistic scenario might be having a bunch of functions that have the same return type and taking the same type/number of arguments, and you want to call different functions based on the value of some variable. Instead of having a bunch of if-statements or a large switch/case you could place the function pointers in an array and use an index to call the appropriate function.

Function Pointers Error In C [duplicate]

This question already has answers here:
How do function pointers in C work?
(12 answers)
Closed 9 years ago.
In a book, I came across the following question.
The problem which I am facing is, what is a function pointer? How does it work and what is the syntax for it's declaration.
Point the error in given code.
main()
{
int (*p)()=fun;
(*p)();
}
fun()
{
printf("Hi..");
}
my first question is, What does following code snipet signifies?
int (*p)()=fun;
(*p)();
and second obvious question what is the error in the given code..??
refer to ionela.voinescu's answer for solution.. it is same as that written in solution manual..thnx
int (*p)() = fun declares a pointer to a function that returns int, then assigns the address of the function fun to that pointer. (*p)() calls whatever function p is pointing to.
Problems with your code:
fun and main should have a return type. Your compiler might not demand it and assume they return ints, but you should give them one nonetheless.
You need to declare fun above main, or use a prototype
Also, (*p)() is unnecessary; you can just use p().
As a side note, because function pointer syntax is relatively ugly, it's fairly common to see typedefs such as
typedef int(*IntFunc)();
which would allow you to declare and use p like so:
IntFunc p = fun;
p();
The correct code is the following:
#include <stdio.h>
int fun();
int main(){
int (*p)()=fun;
(*p)();
return 0;
}
int fun(){
printf("Hi..");
return 0;
}
1.
int fun();
You have to declare your function before using it in main; Otherwise main wont recognize it.
2.
int (*p)() : Declaration of a variable p which is a pointer to a function that returns int and has no arguments(or undefined number of arguments, depending on the standard).
3.
int fun();
.........
int (*p)()=fun;
In order for this to work fun must also return int. Meaning when assigning a value to a variable this must have a type which corresponds with the declaration of the variable. When assigning a value to a pointer to a function that returns int and has no arguments the function assigned to it must also return int and have no arguments (or undefined number of arguments, depending on the standard).
4.
(*p)();
You call the function p which now points to fun.
fun is name of the the function itself.
int (*p)()
is the definition of the function pointer like 'int a' is the definition of an integer.
int * is the pointer itself, () means it is a pointer to a function.
(*p)();
This is the execution of the function of the function pointer p, i.e. p is assigned to the function fun, which is executed by calling the content of the pointer p, thus (*p)();

Resources