Understanding typedefs for function pointers in C

后端 未结 7 1608
别跟我提以往
别跟我提以往 2020-11-22 11:16

I have always been a bit stumped when I read other peoples\' code which had typedefs for pointers to functions with arguments. I recall that it took me a while to get around

7条回答
  •  忘了有多久
    2020-11-22 11:36

    A very easy way to understand typedef of function pointer:

    int add(int a, int b)
    {
        return (a+b);
    }
    
    typedef int (*add_integer)(int, int); //declaration of function pointer
    
    int main()
    {
        add_integer addition = add; //typedef assigns a new variable i.e. "addition" to original function "add"
        int c = addition(11, 11);   //calling function via new variable
        printf("%d",c);
        return 0;
    }
    

提交回复
热议问题