C function pointer syntax











up vote
22
down vote

favorite
3












My question is very simple one.



normally, when declaring some variable, you put its type before it, like:



int a;


a function pointer may have type like: int(*)(int,int), in case we point to a function that takes two int and returns an int. But, when declaring such a pointer, its identifier is not after the type, like:



int(*)(int,int) mypointer;


instead, you must write the identifier in the middle:



int(*mypointer)(int,int);


why is this so?
Sorry, I know it's an embarassingly easy question...



Thanks to everybody for replying.
A.S.










share|improve this question


















  • 3




    That's not the only case, you also use that style in array declarations.
    – effeffe
    Jan 1 '13 at 22:18






  • 2




    Search for "Declaration reflects use".
    – Mr Lister
    Jan 1 '13 at 22:19












  • This Q helps explain Typedef syntax typedef old-type alias-identifier but example function pointer examples like "typedef int (*sum_func)(int,int);" don't match the syntax... Until now!
    – kevinf
    Sep 18 at 0:11

















up vote
22
down vote

favorite
3












My question is very simple one.



normally, when declaring some variable, you put its type before it, like:



int a;


a function pointer may have type like: int(*)(int,int), in case we point to a function that takes two int and returns an int. But, when declaring such a pointer, its identifier is not after the type, like:



int(*)(int,int) mypointer;


instead, you must write the identifier in the middle:



int(*mypointer)(int,int);


why is this so?
Sorry, I know it's an embarassingly easy question...



Thanks to everybody for replying.
A.S.










share|improve this question


















  • 3




    That's not the only case, you also use that style in array declarations.
    – effeffe
    Jan 1 '13 at 22:18






  • 2




    Search for "Declaration reflects use".
    – Mr Lister
    Jan 1 '13 at 22:19












  • This Q helps explain Typedef syntax typedef old-type alias-identifier but example function pointer examples like "typedef int (*sum_func)(int,int);" don't match the syntax... Until now!
    – kevinf
    Sep 18 at 0:11















up vote
22
down vote

favorite
3









up vote
22
down vote

favorite
3






3





My question is very simple one.



normally, when declaring some variable, you put its type before it, like:



int a;


a function pointer may have type like: int(*)(int,int), in case we point to a function that takes two int and returns an int. But, when declaring such a pointer, its identifier is not after the type, like:



int(*)(int,int) mypointer;


instead, you must write the identifier in the middle:



int(*mypointer)(int,int);


why is this so?
Sorry, I know it's an embarassingly easy question...



Thanks to everybody for replying.
A.S.










share|improve this question













My question is very simple one.



normally, when declaring some variable, you put its type before it, like:



int a;


a function pointer may have type like: int(*)(int,int), in case we point to a function that takes two int and returns an int. But, when declaring such a pointer, its identifier is not after the type, like:



int(*)(int,int) mypointer;


instead, you must write the identifier in the middle:



int(*mypointer)(int,int);


why is this so?
Sorry, I know it's an embarassingly easy question...



Thanks to everybody for replying.
A.S.







c declare






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 1 '13 at 22:17









user1941583

114116




114116








  • 3




    That's not the only case, you also use that style in array declarations.
    – effeffe
    Jan 1 '13 at 22:18






  • 2




    Search for "Declaration reflects use".
    – Mr Lister
    Jan 1 '13 at 22:19












  • This Q helps explain Typedef syntax typedef old-type alias-identifier but example function pointer examples like "typedef int (*sum_func)(int,int);" don't match the syntax... Until now!
    – kevinf
    Sep 18 at 0:11
















  • 3




    That's not the only case, you also use that style in array declarations.
    – effeffe
    Jan 1 '13 at 22:18






  • 2




    Search for "Declaration reflects use".
    – Mr Lister
    Jan 1 '13 at 22:19












  • This Q helps explain Typedef syntax typedef old-type alias-identifier but example function pointer examples like "typedef int (*sum_func)(int,int);" don't match the syntax... Until now!
    – kevinf
    Sep 18 at 0:11










3




3




That's not the only case, you also use that style in array declarations.
– effeffe
Jan 1 '13 at 22:18




That's not the only case, you also use that style in array declarations.
– effeffe
Jan 1 '13 at 22:18




2




2




Search for "Declaration reflects use".
– Mr Lister
Jan 1 '13 at 22:19






Search for "Declaration reflects use".
– Mr Lister
Jan 1 '13 at 22:19














This Q helps explain Typedef syntax typedef old-type alias-identifier but example function pointer examples like "typedef int (*sum_func)(int,int);" don't match the syntax... Until now!
– kevinf
Sep 18 at 0:11






This Q helps explain Typedef syntax typedef old-type alias-identifier but example function pointer examples like "typedef int (*sum_func)(int,int);" don't match the syntax... Until now!
– kevinf
Sep 18 at 0:11














4 Answers
4






active

oldest

votes

















up vote
16
down vote













I explain this in my answer to Why was the C syntax for arrays, pointers, and functions designed this way?, and it basically comes down to:




the language authors preferred to make the syntax variable-centric rather than type-centric. That is, they wanted a programmer to look at the declaration and think "if I write the expression *func(arg), that'll result in an int; if I write *arg[N] I'll have a float" rather than "func must be a pointer to a function taking this and returning that".



The C entry on Wikipedia claims that:



Ritchie's idea was to declare identifiers in contexts resembling their use: "declaration reflects use".



...citing p122 of K&R2.







share|improve this answer






























    up vote
    11
    down vote













    This structure reflects how a normal function is declared (and used).



    Consider a normal function definition:



    int foo (int bar, int baz, int quux);


    Now consider defining a function pointer to a function of the same signature:



    int (*foo) (int, int, int);


    Notice how the two structures mirror each other? That makes *foo much easier to identify as a function pointer rather than as something else.






    share|improve this answer




























      up vote
      4
      down vote













      If you're dealing with a function (not a pointer to one), the name is in the middle too. It goes like: return-type function-name "(" argument-list ")" .... For example, in int foo(int), int is the return type, foo the name and int the argument list.



      A pointer to a function works pretty much the same way -- return type, then name, then argument list. In this case, we have to add a * to make it a pointer, and (since the * for a pointer is prefix) a pair of parentheses to bind the * to the name instead of the return type. For example, int *foo(int) would mean a function named foo that takes an int parameter and returns a pointer to an int. To get the * bound to foo instead, we need parentheses, giving int (*foo)(int).



      This gets particularly ugly when you need an array of pointers to functions. In such a case, most people find it easiest to use a typedef for the pointer type, then create an array of that type:



      typedef int (*fptr)(int);

      fptr array[10];





      share|improve this answer




























        up vote
        1
        down vote













        I had seen at some places function pointers are declared as



        int (*foo) (int a, int b);



        and at some places a and b are not mentioned and both still works.



        so int (*foo) (int, int) is also correct.



        Very simple way that I found to remember is as mentioned below



        suppose function is declared as : int function (int a , int b);
        Step1 - simply put function in paranthases : int (function) (int a , int b);
        Step2- Place a * in front of function name and change the name :
        int (*funcPntr) (int a , int b);



        PS :I am not following proper coding guidelines for naming convention etc in this answer.






        share|improve this answer





















          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',
          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/3.0/"u003ecc by-sa 3.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%2f14114749%2fc-function-pointer-syntax%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








          up vote
          16
          down vote













          I explain this in my answer to Why was the C syntax for arrays, pointers, and functions designed this way?, and it basically comes down to:




          the language authors preferred to make the syntax variable-centric rather than type-centric. That is, they wanted a programmer to look at the declaration and think "if I write the expression *func(arg), that'll result in an int; if I write *arg[N] I'll have a float" rather than "func must be a pointer to a function taking this and returning that".



          The C entry on Wikipedia claims that:



          Ritchie's idea was to declare identifiers in contexts resembling their use: "declaration reflects use".



          ...citing p122 of K&R2.







          share|improve this answer



























            up vote
            16
            down vote













            I explain this in my answer to Why was the C syntax for arrays, pointers, and functions designed this way?, and it basically comes down to:




            the language authors preferred to make the syntax variable-centric rather than type-centric. That is, they wanted a programmer to look at the declaration and think "if I write the expression *func(arg), that'll result in an int; if I write *arg[N] I'll have a float" rather than "func must be a pointer to a function taking this and returning that".



            The C entry on Wikipedia claims that:



            Ritchie's idea was to declare identifiers in contexts resembling their use: "declaration reflects use".



            ...citing p122 of K&R2.







            share|improve this answer

























              up vote
              16
              down vote










              up vote
              16
              down vote









              I explain this in my answer to Why was the C syntax for arrays, pointers, and functions designed this way?, and it basically comes down to:




              the language authors preferred to make the syntax variable-centric rather than type-centric. That is, they wanted a programmer to look at the declaration and think "if I write the expression *func(arg), that'll result in an int; if I write *arg[N] I'll have a float" rather than "func must be a pointer to a function taking this and returning that".



              The C entry on Wikipedia claims that:



              Ritchie's idea was to declare identifiers in contexts resembling their use: "declaration reflects use".



              ...citing p122 of K&R2.







              share|improve this answer














              I explain this in my answer to Why was the C syntax for arrays, pointers, and functions designed this way?, and it basically comes down to:




              the language authors preferred to make the syntax variable-centric rather than type-centric. That is, they wanted a programmer to look at the declaration and think "if I write the expression *func(arg), that'll result in an int; if I write *arg[N] I'll have a float" rather than "func must be a pointer to a function taking this and returning that".



              The C entry on Wikipedia claims that:



              Ritchie's idea was to declare identifiers in contexts resembling their use: "declaration reflects use".



              ...citing p122 of K&R2.








              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Apr 12 '17 at 7:31









              Community

              11




              11










              answered Jan 1 '13 at 22:22









              detly

              18.7k762121




              18.7k762121
























                  up vote
                  11
                  down vote













                  This structure reflects how a normal function is declared (and used).



                  Consider a normal function definition:



                  int foo (int bar, int baz, int quux);


                  Now consider defining a function pointer to a function of the same signature:



                  int (*foo) (int, int, int);


                  Notice how the two structures mirror each other? That makes *foo much easier to identify as a function pointer rather than as something else.






                  share|improve this answer

























                    up vote
                    11
                    down vote













                    This structure reflects how a normal function is declared (and used).



                    Consider a normal function definition:



                    int foo (int bar, int baz, int quux);


                    Now consider defining a function pointer to a function of the same signature:



                    int (*foo) (int, int, int);


                    Notice how the two structures mirror each other? That makes *foo much easier to identify as a function pointer rather than as something else.






                    share|improve this answer























                      up vote
                      11
                      down vote










                      up vote
                      11
                      down vote









                      This structure reflects how a normal function is declared (and used).



                      Consider a normal function definition:



                      int foo (int bar, int baz, int quux);


                      Now consider defining a function pointer to a function of the same signature:



                      int (*foo) (int, int, int);


                      Notice how the two structures mirror each other? That makes *foo much easier to identify as a function pointer rather than as something else.






                      share|improve this answer












                      This structure reflects how a normal function is declared (and used).



                      Consider a normal function definition:



                      int foo (int bar, int baz, int quux);


                      Now consider defining a function pointer to a function of the same signature:



                      int (*foo) (int, int, int);


                      Notice how the two structures mirror each other? That makes *foo much easier to identify as a function pointer rather than as something else.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Jan 1 '13 at 22:21









                      atomicinf

                      2,9161117




                      2,9161117






















                          up vote
                          4
                          down vote













                          If you're dealing with a function (not a pointer to one), the name is in the middle too. It goes like: return-type function-name "(" argument-list ")" .... For example, in int foo(int), int is the return type, foo the name and int the argument list.



                          A pointer to a function works pretty much the same way -- return type, then name, then argument list. In this case, we have to add a * to make it a pointer, and (since the * for a pointer is prefix) a pair of parentheses to bind the * to the name instead of the return type. For example, int *foo(int) would mean a function named foo that takes an int parameter and returns a pointer to an int. To get the * bound to foo instead, we need parentheses, giving int (*foo)(int).



                          This gets particularly ugly when you need an array of pointers to functions. In such a case, most people find it easiest to use a typedef for the pointer type, then create an array of that type:



                          typedef int (*fptr)(int);

                          fptr array[10];





                          share|improve this answer

























                            up vote
                            4
                            down vote













                            If you're dealing with a function (not a pointer to one), the name is in the middle too. It goes like: return-type function-name "(" argument-list ")" .... For example, in int foo(int), int is the return type, foo the name and int the argument list.



                            A pointer to a function works pretty much the same way -- return type, then name, then argument list. In this case, we have to add a * to make it a pointer, and (since the * for a pointer is prefix) a pair of parentheses to bind the * to the name instead of the return type. For example, int *foo(int) would mean a function named foo that takes an int parameter and returns a pointer to an int. To get the * bound to foo instead, we need parentheses, giving int (*foo)(int).



                            This gets particularly ugly when you need an array of pointers to functions. In such a case, most people find it easiest to use a typedef for the pointer type, then create an array of that type:



                            typedef int (*fptr)(int);

                            fptr array[10];





                            share|improve this answer























                              up vote
                              4
                              down vote










                              up vote
                              4
                              down vote









                              If you're dealing with a function (not a pointer to one), the name is in the middle too. It goes like: return-type function-name "(" argument-list ")" .... For example, in int foo(int), int is the return type, foo the name and int the argument list.



                              A pointer to a function works pretty much the same way -- return type, then name, then argument list. In this case, we have to add a * to make it a pointer, and (since the * for a pointer is prefix) a pair of parentheses to bind the * to the name instead of the return type. For example, int *foo(int) would mean a function named foo that takes an int parameter and returns a pointer to an int. To get the * bound to foo instead, we need parentheses, giving int (*foo)(int).



                              This gets particularly ugly when you need an array of pointers to functions. In such a case, most people find it easiest to use a typedef for the pointer type, then create an array of that type:



                              typedef int (*fptr)(int);

                              fptr array[10];





                              share|improve this answer












                              If you're dealing with a function (not a pointer to one), the name is in the middle too. It goes like: return-type function-name "(" argument-list ")" .... For example, in int foo(int), int is the return type, foo the name and int the argument list.



                              A pointer to a function works pretty much the same way -- return type, then name, then argument list. In this case, we have to add a * to make it a pointer, and (since the * for a pointer is prefix) a pair of parentheses to bind the * to the name instead of the return type. For example, int *foo(int) would mean a function named foo that takes an int parameter and returns a pointer to an int. To get the * bound to foo instead, we need parentheses, giving int (*foo)(int).



                              This gets particularly ugly when you need an array of pointers to functions. In such a case, most people find it easiest to use a typedef for the pointer type, then create an array of that type:



                              typedef int (*fptr)(int);

                              fptr array[10];






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Jan 1 '13 at 22:22









                              Jerry Coffin

                              381k48460900




                              381k48460900






















                                  up vote
                                  1
                                  down vote













                                  I had seen at some places function pointers are declared as



                                  int (*foo) (int a, int b);



                                  and at some places a and b are not mentioned and both still works.



                                  so int (*foo) (int, int) is also correct.



                                  Very simple way that I found to remember is as mentioned below



                                  suppose function is declared as : int function (int a , int b);
                                  Step1 - simply put function in paranthases : int (function) (int a , int b);
                                  Step2- Place a * in front of function name and change the name :
                                  int (*funcPntr) (int a , int b);



                                  PS :I am not following proper coding guidelines for naming convention etc in this answer.






                                  share|improve this answer

























                                    up vote
                                    1
                                    down vote













                                    I had seen at some places function pointers are declared as



                                    int (*foo) (int a, int b);



                                    and at some places a and b are not mentioned and both still works.



                                    so int (*foo) (int, int) is also correct.



                                    Very simple way that I found to remember is as mentioned below



                                    suppose function is declared as : int function (int a , int b);
                                    Step1 - simply put function in paranthases : int (function) (int a , int b);
                                    Step2- Place a * in front of function name and change the name :
                                    int (*funcPntr) (int a , int b);



                                    PS :I am not following proper coding guidelines for naming convention etc in this answer.






                                    share|improve this answer























                                      up vote
                                      1
                                      down vote










                                      up vote
                                      1
                                      down vote









                                      I had seen at some places function pointers are declared as



                                      int (*foo) (int a, int b);



                                      and at some places a and b are not mentioned and both still works.



                                      so int (*foo) (int, int) is also correct.



                                      Very simple way that I found to remember is as mentioned below



                                      suppose function is declared as : int function (int a , int b);
                                      Step1 - simply put function in paranthases : int (function) (int a , int b);
                                      Step2- Place a * in front of function name and change the name :
                                      int (*funcPntr) (int a , int b);



                                      PS :I am not following proper coding guidelines for naming convention etc in this answer.






                                      share|improve this answer












                                      I had seen at some places function pointers are declared as



                                      int (*foo) (int a, int b);



                                      and at some places a and b are not mentioned and both still works.



                                      so int (*foo) (int, int) is also correct.



                                      Very simple way that I found to remember is as mentioned below



                                      suppose function is declared as : int function (int a , int b);
                                      Step1 - simply put function in paranthases : int (function) (int a , int b);
                                      Step2- Place a * in front of function name and change the name :
                                      int (*funcPntr) (int a , int b);



                                      PS :I am not following proper coding guidelines for naming convention etc in this answer.







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Oct 20 '15 at 3:24









                                      Vikas Kumar Sinha

                                      111




                                      111






























                                           

                                          draft saved


                                          draft discarded



















































                                           


                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function () {
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f14114749%2fc-function-pointer-syntax%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

                                          How to pass form data using jquery Ajax to insert data in database?

                                          National Museum of Racing and Hall of Fame

                                          Guess what letter conforming each word