简体   繁体   English

如何为函数指针创建typedef

[英]How to create a typedef for function pointers

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. 我认为如果我为函数指针创建了一个typedef会更容易使用函数指针,但我似乎让自己绊倒了一些语法或用法或类似于函数指针的typedef,我可以使用一些帮助。

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. 这基本上就是我想要实现的,我是一个相当新的C程序员,这让我太过分了。 What replaces <???> ? 什么取代<???>

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

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: 在您的情况下,您可以将函数指针g定义为:

typedef int (*g)(int); // typedef of the function pointer. //函数指针的typedef

g is a function pointer for the function returning int value and taking one int argument. g是函数返回int值并取一个int参数的函数指针。

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: OUTPUT:

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

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. 这里call是一个函数,它只取回一个函数指针,它接受2个参数并返回一个整数值。 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM