41

I think it would be easier to use function pointers if I created a typedef for a function pointer, but I seem to be getting myself tripped up on some syntax or usage or something about typedef for function pointers, and I could use some help.

I've got

int foo(int i){ return i + 1;}
typedef <???> g;
int hvar;
hvar = g(3)

That's basically what I'm trying to accomplish I'm a rather new C programmer and this is throwing me too much. What replaces <???> ?

5
  • 3
    Your code can't possibly work because if g is a typedef, you can't call it as a function. Can you describe what specifically you're trying to do? Commented Jun 14, 2012 at 17:40
  • Really? I thought my boss had told me to use a typedef to make this work. Can you accomplish somehing similar with #Define ? Commented Jun 14, 2012 at 17:41
  • 1
    What specifically are your trying to do? It's not clear what you're attempting to do. Commented Jun 14, 2012 at 17:42
  • By using define you can do #define g foo and then int hvar; hvar = g(3); Commented Jun 14, 2012 at 17:43
  • 1
    You must first do the typedef (a function pointer, I presume) and than use that typedef to define a variable (of type pointer to function) initialise the pointer with the function you want, and than use that variable to invoke the function that it ponts to. Commented Jun 14, 2012 at 17:43

3 Answers 3

90

Your question isn't clear, but I think you might want something like this:

int foo(int i){ return i + 1;}

typedef int (*g)(int);  // Declare typedef

g func = &foo;          // Define function-pointer variable, and initialise

int hvar = func(3);     // Call function through pointer
Sign up to request clarification or add additional context in comments.

2 Comments

It’s also possible to typedef the function type, which is somewhat uncommon, but may be clearer for illustration: typedef int g(int); g foo; g *func = &foo;. This is useful for declaring (unfortunately, not defining) multiple functions with the same signature.
@JonPurdy suggest posting that as an answer
8

You are right. The function pointer can be conveniently used to point to the different functions of the same return type and taking same number of arguments. The argument types should match the declaration of the function pointer arguments.

In your case you could define your function pointer g as:

typedef int (*g)(int); // typedef of the function pointer.

g is a function pointer for the function returning int value and taking one int argument.

The usage of function pointer could be illustrated by a simple program below:

#include<stdio.h>

typedef int (*pointer_to_function)(int first_parameter_of_type_int, int second_parameter_of_type_int);

int my_function_returning_int_and_taking_two_int_arguments(int par1, int par2)
{
    int result = par1 + par2;
    return result;
}

int my_mul_function(int par1, int par2)
{
    int result = par1 * par2;
    return result;
}

int main()
{
    int res; // returning result will be here
    pointer_to_function my_fun_pointer; // declare function pointer variable;

    my_fun_pointer = my_function_returning_int_and_taking_two_int_arguments; // function pointer points to `my_function_returning_int_and_taking_two_int_arguments` function
    res = my_fun_pointer(2,3);       // Call function through pointer
    printf(" result of `my_function_returning_int_and_taking_two_int_arguments` = %d \n", res);


    my_fun_pointer = my_mul_function; // now function pointer points to another function: `my_mul_function` 
    res = my_fun_pointer(2,3);       // Call function through pointer
    printf(" result of `my_mul_function` = %d \n", res);

   return 0;
}

OUTPUT:

result of `my_function_returning_int_and_taking_two_int_arguments` = 5 
result of `my_mul_function` = 6 

Comments

0

The original way of writing the function returning function pointer is

int (* call(void) ) (int,int);

Here call is a function which takes nothing but returns a function pointer which takes 2 arguments and returns an integer value. Pay attention to the brackets, they are absolutely necessary.

Here is the code:

#include<stdio.h>

int sum(int a,int b)   //sum is the function returned by call
{
   return a+b;
}

int (*call(void) ) (int ,int);

int main() {
  int (*p)(int,int);   // way to declare a function pointer
  p=call();
  printf("%d\n",(*p)(8,3));
}

int( *call(void) )(int,int) {
    return sum;
}

1 Comment

How is this related to the question?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.