Defining a function which returns a function pointer which also returns a function pointer without...

How does Vivi differ from other Black Mages?

Do Milankovitch Cycles fully explain climate change?

Is Salesforce Classic being deprecated?

How can I fix a framing mistake so I can drywall?

Georgian capital letter “Ⴒ” (“tar”) in pdfLaTeX

What's the biggest organic molecule that could have a smell?

Why did it become so much more expensive to start a university?

The case of the pranking snowplow

Why should I always enable compiler warnings?

Does a gnoll speak both Gnoll and Abyssal, or is Gnoll a dialect of Abyssal?

Are programming languages necessary/useful for operations research practitioner?

What is Japanese Language Stack Exchange called in Japanese?

I asked for a graduate student position from a professor. He replied "welcome". What does that mean?

Do ibuprofen or paracetamol cause hearing loss?

Job offer without any details but asking me to withdraw other applications - is it normal?

Are Democrats more likely to believe Astrology is a science?

Can I disable a battery powered device by reversing half of its batteries?

Random point on a sphere

Where does the expression "triple-A" comes from?

Can I toggle Do Not Disturb on/off on my Mac as easily as I can on my iPhone?

My employer wants me to do a work of 6 months in just 2 months

Can the UK veto its own extension request?

Which ping implementation is Cygwin using?

Writing a worded mathematical expression



Defining a function which returns a function pointer which also returns a function pointer without typedefs


How do function pointers in C work?Understanding typedefs for function pointers in CTypedef function pointer?Returning function pointer typetypedef function pointers and extern keywordError with function pointer returning a (void *)Passing capturing lambda as function pointerPass typedefed function pointerReturn a function pointer without a typedef






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







8















I am trying to really understand function pointers without using typedef but cannot seem to get this. I do not understand what signature is needed to convey that I return a pointer to a pointer to a function.



#include <stdio.h>

void odd() { printf("odd!n"); }
void even() { printf("even!n"); }

void (*get_pfn(int i))()
{
return i % 2 == 0 ? &even : &odd;
}

__SIGNATURE__
{
return &get_pfn;
}

int main()
{
get_pfn_pfn()(1)();
get_pfn_pfn()(2)();
return 0;
}


To make this work, what does __SIGNATURE__ have to be?










share|improve this question






















  • 6





    Good question - now don't ever do this again. :)

    – Michael Dorgan
    11 hours ago






  • 5





    use cdecl

    – Antti Haapala
    10 hours ago




















8















I am trying to really understand function pointers without using typedef but cannot seem to get this. I do not understand what signature is needed to convey that I return a pointer to a pointer to a function.



#include <stdio.h>

void odd() { printf("odd!n"); }
void even() { printf("even!n"); }

void (*get_pfn(int i))()
{
return i % 2 == 0 ? &even : &odd;
}

__SIGNATURE__
{
return &get_pfn;
}

int main()
{
get_pfn_pfn()(1)();
get_pfn_pfn()(2)();
return 0;
}


To make this work, what does __SIGNATURE__ have to be?










share|improve this question






















  • 6





    Good question - now don't ever do this again. :)

    – Michael Dorgan
    11 hours ago






  • 5





    use cdecl

    – Antti Haapala
    10 hours ago
















8












8








8


2






I am trying to really understand function pointers without using typedef but cannot seem to get this. I do not understand what signature is needed to convey that I return a pointer to a pointer to a function.



#include <stdio.h>

void odd() { printf("odd!n"); }
void even() { printf("even!n"); }

void (*get_pfn(int i))()
{
return i % 2 == 0 ? &even : &odd;
}

__SIGNATURE__
{
return &get_pfn;
}

int main()
{
get_pfn_pfn()(1)();
get_pfn_pfn()(2)();
return 0;
}


To make this work, what does __SIGNATURE__ have to be?










share|improve this question
















I am trying to really understand function pointers without using typedef but cannot seem to get this. I do not understand what signature is needed to convey that I return a pointer to a pointer to a function.



#include <stdio.h>

void odd() { printf("odd!n"); }
void even() { printf("even!n"); }

void (*get_pfn(int i))()
{
return i % 2 == 0 ? &even : &odd;
}

__SIGNATURE__
{
return &get_pfn;
}

int main()
{
get_pfn_pfn()(1)();
get_pfn_pfn()(2)();
return 0;
}


To make this work, what does __SIGNATURE__ have to be?







c function function-pointers






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 hours ago









Justin

15.3k10 gold badges63 silver badges106 bronze badges




15.3k10 gold badges63 silver badges106 bronze badges










asked 11 hours ago









JasJas

5095 silver badges14 bronze badges




5095 silver badges14 bronze badges











  • 6





    Good question - now don't ever do this again. :)

    – Michael Dorgan
    11 hours ago






  • 5





    use cdecl

    – Antti Haapala
    10 hours ago
















  • 6





    Good question - now don't ever do this again. :)

    – Michael Dorgan
    11 hours ago






  • 5





    use cdecl

    – Antti Haapala
    10 hours ago










6




6





Good question - now don't ever do this again. :)

– Michael Dorgan
11 hours ago





Good question - now don't ever do this again. :)

– Michael Dorgan
11 hours ago




5




5





use cdecl

– Antti Haapala
10 hours ago







use cdecl

– Antti Haapala
10 hours ago














4 Answers
4






active

oldest

votes


















9
















It has to return a function pointer to a function that takes an int and returns a function pointer:



void (*(*get_pfn_pfn(void))(int))(void) {
return &get_pfn;
}


more lines:



void (*
(*
get_pfn_pfn(void) // this is our function
)(int i) // this is get_pfn(int)
)(void) // this is odd() or even()
{
return &get_pfn;
}


The voids can be omitted, in which case the function pointer points to a function that takes unknown number of parameters. Which is not what you want. To declare a function pointer to a function that takes no arguments, you should add void inside function parameter list. The same way it's best to change get_pfn to void (*get_pfn(int i))(void). For example try calling from get_pfn(1)("some arg", "some other arg");. A C compiler will not give a warning, as empty () denote unknown arguments. To say that function takes no arguments, you have to (void).



For many the sequences of braces, especially ))(, in function pointers are hard to parse. That's why many prefer typedefs for function pointers or types:



typedef void get_pfn_func_t(void);    
get_pfn_func_t *get_pfn(int i) {
return i % 2 == 0 ? &even : &odd;
}

typedef get_pfn_func_t *get_pfn_pfn_func_t(int i);
get_pfn_pfn_func_t *get_pfn_pfn(void) {
return &get_pfn;
}





share|improve this answer























  • 2





    This one confirmed to work with online C check.

    – Michael Dorgan
    11 hours ago











  • Thank you so so much Kamil!! I like that broken down/visualized tree like view, it helped make it a lot more understandable :) !!

    – Jas
    11 hours ago








  • 1





    @Jas You may like it but I don't think it is recommended to write code in this manner.

    – machine_1
    11 hours ago











  • I wish there was a website that had a GUI representation of a function pointer, let you move various components around, etc. Something like nsdateformatter.com

    – Alexander
    2 hours ago



















4
















The return type of the function get_pfn is -



void (*) ();


So type of &get_pfn is -



void (*(*)(int))()


Now, this function returns this type, hence it's signature will be -



void (*(*(foo)())(int))()


You can verify this by typing this in cdecl.org






share|improve this answer




























  • Thank you very much Ajay! Your explanation breaking down the steps helped me understand it more easily, but unfortunately the signature did not work sorry :(

    – Jas
    11 hours ago






  • 1





    @Jas, sorry I messed up the order of the (int). I thought your odd even functions take a param int. I will correct it.

    – Ajay Brahmakshatriya
    11 hours ago






  • 1





    @Jas, I have corrected my answer,

    – Ajay Brahmakshatriya
    11 hours ago



















2
















Function pointers without a typedef can be tricky to work with. To figure them out, you work from the inside out.



So let's break down exactly how we come up with the correct function signature.



get_pfn_pfn is a function:



get_pfn_pfn()


Which takes no parameters:



get_pfn_pfn(void)


And returns a pointer:



*get_pfn_pfn(void)


To a function:



(*get_pfn_pfn(void))()


Which takes an int parameter:



(*get_pfn_pfn(void))(int)


And returns a pointer:



*(*get_pfn_pfn(void))(int)


To a function:



(*(*get_pfn_pfn(void))(int))()


Which takes no parameters:



(*(*get_pfn_pfn(void))(int))(void)


And returns nothing (i.e. void):



void (*(*get_pfn_pfn(void))(int))(void)




Of course, using typedef's simplifies this greatly.



First the type for even and odd:



typedef void (*func1)(void);


Which we can then apply to get_pfn:



func1 get_pfn(int) { ... }


Then the type for this function:



typedef func1 (*func2)(int);


Which we can apply to get_pfn_pfn:



func2 get_pfn_pfn(void) { ... }





share|improve this answer



































    1
















    it's this way:



    void (*(*get_pfn_pfn(void))(int))()





    share|improve this answer










    New contributor



    Abdelfettah Besbes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.
























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });















      draft saved

      draft discarded
















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f57875417%2fdefining-a-function-which-returns-a-function-pointer-which-also-returns-a-functi%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      9
















      It has to return a function pointer to a function that takes an int and returns a function pointer:



      void (*(*get_pfn_pfn(void))(int))(void) {
      return &get_pfn;
      }


      more lines:



      void (*
      (*
      get_pfn_pfn(void) // this is our function
      )(int i) // this is get_pfn(int)
      )(void) // this is odd() or even()
      {
      return &get_pfn;
      }


      The voids can be omitted, in which case the function pointer points to a function that takes unknown number of parameters. Which is not what you want. To declare a function pointer to a function that takes no arguments, you should add void inside function parameter list. The same way it's best to change get_pfn to void (*get_pfn(int i))(void). For example try calling from get_pfn(1)("some arg", "some other arg");. A C compiler will not give a warning, as empty () denote unknown arguments. To say that function takes no arguments, you have to (void).



      For many the sequences of braces, especially ))(, in function pointers are hard to parse. That's why many prefer typedefs for function pointers or types:



      typedef void get_pfn_func_t(void);    
      get_pfn_func_t *get_pfn(int i) {
      return i % 2 == 0 ? &even : &odd;
      }

      typedef get_pfn_func_t *get_pfn_pfn_func_t(int i);
      get_pfn_pfn_func_t *get_pfn_pfn(void) {
      return &get_pfn;
      }





      share|improve this answer























      • 2





        This one confirmed to work with online C check.

        – Michael Dorgan
        11 hours ago











      • Thank you so so much Kamil!! I like that broken down/visualized tree like view, it helped make it a lot more understandable :) !!

        – Jas
        11 hours ago








      • 1





        @Jas You may like it but I don't think it is recommended to write code in this manner.

        – machine_1
        11 hours ago











      • I wish there was a website that had a GUI representation of a function pointer, let you move various components around, etc. Something like nsdateformatter.com

        – Alexander
        2 hours ago
















      9
















      It has to return a function pointer to a function that takes an int and returns a function pointer:



      void (*(*get_pfn_pfn(void))(int))(void) {
      return &get_pfn;
      }


      more lines:



      void (*
      (*
      get_pfn_pfn(void) // this is our function
      )(int i) // this is get_pfn(int)
      )(void) // this is odd() or even()
      {
      return &get_pfn;
      }


      The voids can be omitted, in which case the function pointer points to a function that takes unknown number of parameters. Which is not what you want. To declare a function pointer to a function that takes no arguments, you should add void inside function parameter list. The same way it's best to change get_pfn to void (*get_pfn(int i))(void). For example try calling from get_pfn(1)("some arg", "some other arg");. A C compiler will not give a warning, as empty () denote unknown arguments. To say that function takes no arguments, you have to (void).



      For many the sequences of braces, especially ))(, in function pointers are hard to parse. That's why many prefer typedefs for function pointers or types:



      typedef void get_pfn_func_t(void);    
      get_pfn_func_t *get_pfn(int i) {
      return i % 2 == 0 ? &even : &odd;
      }

      typedef get_pfn_func_t *get_pfn_pfn_func_t(int i);
      get_pfn_pfn_func_t *get_pfn_pfn(void) {
      return &get_pfn;
      }





      share|improve this answer























      • 2





        This one confirmed to work with online C check.

        – Michael Dorgan
        11 hours ago











      • Thank you so so much Kamil!! I like that broken down/visualized tree like view, it helped make it a lot more understandable :) !!

        – Jas
        11 hours ago








      • 1





        @Jas You may like it but I don't think it is recommended to write code in this manner.

        – machine_1
        11 hours ago











      • I wish there was a website that had a GUI representation of a function pointer, let you move various components around, etc. Something like nsdateformatter.com

        – Alexander
        2 hours ago














      9














      9










      9









      It has to return a function pointer to a function that takes an int and returns a function pointer:



      void (*(*get_pfn_pfn(void))(int))(void) {
      return &get_pfn;
      }


      more lines:



      void (*
      (*
      get_pfn_pfn(void) // this is our function
      )(int i) // this is get_pfn(int)
      )(void) // this is odd() or even()
      {
      return &get_pfn;
      }


      The voids can be omitted, in which case the function pointer points to a function that takes unknown number of parameters. Which is not what you want. To declare a function pointer to a function that takes no arguments, you should add void inside function parameter list. The same way it's best to change get_pfn to void (*get_pfn(int i))(void). For example try calling from get_pfn(1)("some arg", "some other arg");. A C compiler will not give a warning, as empty () denote unknown arguments. To say that function takes no arguments, you have to (void).



      For many the sequences of braces, especially ))(, in function pointers are hard to parse. That's why many prefer typedefs for function pointers or types:



      typedef void get_pfn_func_t(void);    
      get_pfn_func_t *get_pfn(int i) {
      return i % 2 == 0 ? &even : &odd;
      }

      typedef get_pfn_func_t *get_pfn_pfn_func_t(int i);
      get_pfn_pfn_func_t *get_pfn_pfn(void) {
      return &get_pfn;
      }





      share|improve this answer















      It has to return a function pointer to a function that takes an int and returns a function pointer:



      void (*(*get_pfn_pfn(void))(int))(void) {
      return &get_pfn;
      }


      more lines:



      void (*
      (*
      get_pfn_pfn(void) // this is our function
      )(int i) // this is get_pfn(int)
      )(void) // this is odd() or even()
      {
      return &get_pfn;
      }


      The voids can be omitted, in which case the function pointer points to a function that takes unknown number of parameters. Which is not what you want. To declare a function pointer to a function that takes no arguments, you should add void inside function parameter list. The same way it's best to change get_pfn to void (*get_pfn(int i))(void). For example try calling from get_pfn(1)("some arg", "some other arg");. A C compiler will not give a warning, as empty () denote unknown arguments. To say that function takes no arguments, you have to (void).



      For many the sequences of braces, especially ))(, in function pointers are hard to parse. That's why many prefer typedefs for function pointers or types:



      typedef void get_pfn_func_t(void);    
      get_pfn_func_t *get_pfn(int i) {
      return i % 2 == 0 ? &even : &odd;
      }

      typedef get_pfn_func_t *get_pfn_pfn_func_t(int i);
      get_pfn_pfn_func_t *get_pfn_pfn(void) {
      return &get_pfn;
      }






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited 11 hours ago

























      answered 11 hours ago









      Kamil CukKamil Cuk

      21.5k2 gold badges9 silver badges37 bronze badges




      21.5k2 gold badges9 silver badges37 bronze badges











      • 2





        This one confirmed to work with online C check.

        – Michael Dorgan
        11 hours ago











      • Thank you so so much Kamil!! I like that broken down/visualized tree like view, it helped make it a lot more understandable :) !!

        – Jas
        11 hours ago








      • 1





        @Jas You may like it but I don't think it is recommended to write code in this manner.

        – machine_1
        11 hours ago











      • I wish there was a website that had a GUI representation of a function pointer, let you move various components around, etc. Something like nsdateformatter.com

        – Alexander
        2 hours ago














      • 2





        This one confirmed to work with online C check.

        – Michael Dorgan
        11 hours ago











      • Thank you so so much Kamil!! I like that broken down/visualized tree like view, it helped make it a lot more understandable :) !!

        – Jas
        11 hours ago








      • 1





        @Jas You may like it but I don't think it is recommended to write code in this manner.

        – machine_1
        11 hours ago











      • I wish there was a website that had a GUI representation of a function pointer, let you move various components around, etc. Something like nsdateformatter.com

        – Alexander
        2 hours ago








      2




      2





      This one confirmed to work with online C check.

      – Michael Dorgan
      11 hours ago





      This one confirmed to work with online C check.

      – Michael Dorgan
      11 hours ago













      Thank you so so much Kamil!! I like that broken down/visualized tree like view, it helped make it a lot more understandable :) !!

      – Jas
      11 hours ago







      Thank you so so much Kamil!! I like that broken down/visualized tree like view, it helped make it a lot more understandable :) !!

      – Jas
      11 hours ago






      1




      1





      @Jas You may like it but I don't think it is recommended to write code in this manner.

      – machine_1
      11 hours ago





      @Jas You may like it but I don't think it is recommended to write code in this manner.

      – machine_1
      11 hours ago













      I wish there was a website that had a GUI representation of a function pointer, let you move various components around, etc. Something like nsdateformatter.com

      – Alexander
      2 hours ago





      I wish there was a website that had a GUI representation of a function pointer, let you move various components around, etc. Something like nsdateformatter.com

      – Alexander
      2 hours ago













      4
















      The return type of the function get_pfn is -



      void (*) ();


      So type of &get_pfn is -



      void (*(*)(int))()


      Now, this function returns this type, hence it's signature will be -



      void (*(*(foo)())(int))()


      You can verify this by typing this in cdecl.org






      share|improve this answer




























      • Thank you very much Ajay! Your explanation breaking down the steps helped me understand it more easily, but unfortunately the signature did not work sorry :(

        – Jas
        11 hours ago






      • 1





        @Jas, sorry I messed up the order of the (int). I thought your odd even functions take a param int. I will correct it.

        – Ajay Brahmakshatriya
        11 hours ago






      • 1





        @Jas, I have corrected my answer,

        – Ajay Brahmakshatriya
        11 hours ago
















      4
















      The return type of the function get_pfn is -



      void (*) ();


      So type of &get_pfn is -



      void (*(*)(int))()


      Now, this function returns this type, hence it's signature will be -



      void (*(*(foo)())(int))()


      You can verify this by typing this in cdecl.org






      share|improve this answer




























      • Thank you very much Ajay! Your explanation breaking down the steps helped me understand it more easily, but unfortunately the signature did not work sorry :(

        – Jas
        11 hours ago






      • 1





        @Jas, sorry I messed up the order of the (int). I thought your odd even functions take a param int. I will correct it.

        – Ajay Brahmakshatriya
        11 hours ago






      • 1





        @Jas, I have corrected my answer,

        – Ajay Brahmakshatriya
        11 hours ago














      4














      4










      4









      The return type of the function get_pfn is -



      void (*) ();


      So type of &get_pfn is -



      void (*(*)(int))()


      Now, this function returns this type, hence it's signature will be -



      void (*(*(foo)())(int))()


      You can verify this by typing this in cdecl.org






      share|improve this answer















      The return type of the function get_pfn is -



      void (*) ();


      So type of &get_pfn is -



      void (*(*)(int))()


      Now, this function returns this type, hence it's signature will be -



      void (*(*(foo)())(int))()


      You can verify this by typing this in cdecl.org







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited 11 hours ago

























      answered 11 hours ago









      Ajay BrahmakshatriyaAjay Brahmakshatriya

      7,2453 gold badges16 silver badges36 bronze badges




      7,2453 gold badges16 silver badges36 bronze badges
















      • Thank you very much Ajay! Your explanation breaking down the steps helped me understand it more easily, but unfortunately the signature did not work sorry :(

        – Jas
        11 hours ago






      • 1





        @Jas, sorry I messed up the order of the (int). I thought your odd even functions take a param int. I will correct it.

        – Ajay Brahmakshatriya
        11 hours ago






      • 1





        @Jas, I have corrected my answer,

        – Ajay Brahmakshatriya
        11 hours ago



















      • Thank you very much Ajay! Your explanation breaking down the steps helped me understand it more easily, but unfortunately the signature did not work sorry :(

        – Jas
        11 hours ago






      • 1





        @Jas, sorry I messed up the order of the (int). I thought your odd even functions take a param int. I will correct it.

        – Ajay Brahmakshatriya
        11 hours ago






      • 1





        @Jas, I have corrected my answer,

        – Ajay Brahmakshatriya
        11 hours ago

















      Thank you very much Ajay! Your explanation breaking down the steps helped me understand it more easily, but unfortunately the signature did not work sorry :(

      – Jas
      11 hours ago





      Thank you very much Ajay! Your explanation breaking down the steps helped me understand it more easily, but unfortunately the signature did not work sorry :(

      – Jas
      11 hours ago




      1




      1





      @Jas, sorry I messed up the order of the (int). I thought your odd even functions take a param int. I will correct it.

      – Ajay Brahmakshatriya
      11 hours ago





      @Jas, sorry I messed up the order of the (int). I thought your odd even functions take a param int. I will correct it.

      – Ajay Brahmakshatriya
      11 hours ago




      1




      1





      @Jas, I have corrected my answer,

      – Ajay Brahmakshatriya
      11 hours ago





      @Jas, I have corrected my answer,

      – Ajay Brahmakshatriya
      11 hours ago











      2
















      Function pointers without a typedef can be tricky to work with. To figure them out, you work from the inside out.



      So let's break down exactly how we come up with the correct function signature.



      get_pfn_pfn is a function:



      get_pfn_pfn()


      Which takes no parameters:



      get_pfn_pfn(void)


      And returns a pointer:



      *get_pfn_pfn(void)


      To a function:



      (*get_pfn_pfn(void))()


      Which takes an int parameter:



      (*get_pfn_pfn(void))(int)


      And returns a pointer:



      *(*get_pfn_pfn(void))(int)


      To a function:



      (*(*get_pfn_pfn(void))(int))()


      Which takes no parameters:



      (*(*get_pfn_pfn(void))(int))(void)


      And returns nothing (i.e. void):



      void (*(*get_pfn_pfn(void))(int))(void)




      Of course, using typedef's simplifies this greatly.



      First the type for even and odd:



      typedef void (*func1)(void);


      Which we can then apply to get_pfn:



      func1 get_pfn(int) { ... }


      Then the type for this function:



      typedef func1 (*func2)(int);


      Which we can apply to get_pfn_pfn:



      func2 get_pfn_pfn(void) { ... }





      share|improve this answer
































        2
















        Function pointers without a typedef can be tricky to work with. To figure them out, you work from the inside out.



        So let's break down exactly how we come up with the correct function signature.



        get_pfn_pfn is a function:



        get_pfn_pfn()


        Which takes no parameters:



        get_pfn_pfn(void)


        And returns a pointer:



        *get_pfn_pfn(void)


        To a function:



        (*get_pfn_pfn(void))()


        Which takes an int parameter:



        (*get_pfn_pfn(void))(int)


        And returns a pointer:



        *(*get_pfn_pfn(void))(int)


        To a function:



        (*(*get_pfn_pfn(void))(int))()


        Which takes no parameters:



        (*(*get_pfn_pfn(void))(int))(void)


        And returns nothing (i.e. void):



        void (*(*get_pfn_pfn(void))(int))(void)




        Of course, using typedef's simplifies this greatly.



        First the type for even and odd:



        typedef void (*func1)(void);


        Which we can then apply to get_pfn:



        func1 get_pfn(int) { ... }


        Then the type for this function:



        typedef func1 (*func2)(int);


        Which we can apply to get_pfn_pfn:



        func2 get_pfn_pfn(void) { ... }





        share|improve this answer






























          2














          2










          2









          Function pointers without a typedef can be tricky to work with. To figure them out, you work from the inside out.



          So let's break down exactly how we come up with the correct function signature.



          get_pfn_pfn is a function:



          get_pfn_pfn()


          Which takes no parameters:



          get_pfn_pfn(void)


          And returns a pointer:



          *get_pfn_pfn(void)


          To a function:



          (*get_pfn_pfn(void))()


          Which takes an int parameter:



          (*get_pfn_pfn(void))(int)


          And returns a pointer:



          *(*get_pfn_pfn(void))(int)


          To a function:



          (*(*get_pfn_pfn(void))(int))()


          Which takes no parameters:



          (*(*get_pfn_pfn(void))(int))(void)


          And returns nothing (i.e. void):



          void (*(*get_pfn_pfn(void))(int))(void)




          Of course, using typedef's simplifies this greatly.



          First the type for even and odd:



          typedef void (*func1)(void);


          Which we can then apply to get_pfn:



          func1 get_pfn(int) { ... }


          Then the type for this function:



          typedef func1 (*func2)(int);


          Which we can apply to get_pfn_pfn:



          func2 get_pfn_pfn(void) { ... }





          share|improve this answer















          Function pointers without a typedef can be tricky to work with. To figure them out, you work from the inside out.



          So let's break down exactly how we come up with the correct function signature.



          get_pfn_pfn is a function:



          get_pfn_pfn()


          Which takes no parameters:



          get_pfn_pfn(void)


          And returns a pointer:



          *get_pfn_pfn(void)


          To a function:



          (*get_pfn_pfn(void))()


          Which takes an int parameter:



          (*get_pfn_pfn(void))(int)


          And returns a pointer:



          *(*get_pfn_pfn(void))(int)


          To a function:



          (*(*get_pfn_pfn(void))(int))()


          Which takes no parameters:



          (*(*get_pfn_pfn(void))(int))(void)


          And returns nothing (i.e. void):



          void (*(*get_pfn_pfn(void))(int))(void)




          Of course, using typedef's simplifies this greatly.



          First the type for even and odd:



          typedef void (*func1)(void);


          Which we can then apply to get_pfn:



          func1 get_pfn(int) { ... }


          Then the type for this function:



          typedef func1 (*func2)(int);


          Which we can apply to get_pfn_pfn:



          func2 get_pfn_pfn(void) { ... }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 10 hours ago

























          answered 11 hours ago









          dbushdbush

          112k15 gold badges127 silver badges159 bronze badges




          112k15 gold badges127 silver badges159 bronze badges


























              1
















              it's this way:



              void (*(*get_pfn_pfn(void))(int))()





              share|improve this answer










              New contributor



              Abdelfettah Besbes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.


























                1
















                it's this way:



                void (*(*get_pfn_pfn(void))(int))()





                share|improve this answer










                New contributor



                Abdelfettah Besbes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.
























                  1














                  1










                  1









                  it's this way:



                  void (*(*get_pfn_pfn(void))(int))()





                  share|improve this answer










                  New contributor



                  Abdelfettah Besbes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  it's this way:



                  void (*(*get_pfn_pfn(void))(int))()






                  share|improve this answer










                  New contributor



                  Abdelfettah Besbes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.








                  share|improve this answer



                  share|improve this answer








                  edited 11 hours ago





















                  New contributor



                  Abdelfettah Besbes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.








                  answered 11 hours ago









                  Abdelfettah BesbesAbdelfettah Besbes

                  31110 bronze badges




                  31110 bronze badges




                  New contributor



                  Abdelfettah Besbes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.




                  New contributor




                  Abdelfettah Besbes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.




































                      draft saved

                      draft discarded



















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f57875417%2fdefining-a-function-which-returns-a-function-pointer-which-also-returns-a-functi%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

                      Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

                      Nicolae Petrescu-Găină Cuprins Biografie | Opera | In memoriam | Varia | Controverse, incertitudini...