Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. Function pointer syntax help

Function pointer syntax help

Scheduled Pinned Locked Moved ATL / WTL / STL
helpquestion
14 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Xpnctoc

    Can someone tell me what's wrong with this code:

    #include typedef void (*VoidFunctionPtr)(void);

    void FunctionA()
    {
    printf("You are in Function A.\n");
    }

    void FunctionB()
    {
    printf("You are in Function B.\n");
    }

    int main(void)
    {
    VoidFunctionPtr fp = FunctionA;
    fp();
    }

    In main(), the declaration of variable "fp" throws a C2275 error: "'VoidFunctionPtr': illegal use of this type as an expression." I don't get it. I have cross-referenced several books and internet articles. All show the same syntax. What am I missing?

    X Offline
    X Offline
    Xpnctoc
    wrote on last edited by
    #5

    I found the problem. I only posted a limited version of the code to focus the direction of the inquiry. In the context of the full code, what is going on is that the compiler expects all variables to be declared at the beginning of the function. Original code:

    #include typedef void (*VoidFunctionPtr)(void);

    void FunctionA(void)
    {
    printf("You are in Function A.\n");
    }

    int main(void)
    {
    printf("Here is a program that does something.\n");
    VoidFunctionPtr fp = FunctionA; // FAILS because variables must be declared first.
    fp();
    }

    Revised code:

    #include typedef void (*VoidFunctionPtr)(void);

    void FunctionA(void)
    {
    printf("You are in Function A.\n");
    }

    int main(void)
    {
    VoidFunctionPtr fp = FunctionA; // MOVED variable declaration to first line.

    printf("Here is a program that does something.\\n");
    fp();
    

    }

    I'm sure this wouldn't happen in a C++ project, but I am using compiler settings to force Standard C compilation. I come from a C++ background but I'm trying to learn the differences between C++ and standard C. I bought a book on standard C, but it references the "C99" standard, which apparently allows variables to be defined anywhere in a function. I guess the MS C compiler works on an older standard?

    A M 3 Replies Last reply
    0
    • X Xpnctoc

      I found the problem. I only posted a limited version of the code to focus the direction of the inquiry. In the context of the full code, what is going on is that the compiler expects all variables to be declared at the beginning of the function. Original code:

      #include typedef void (*VoidFunctionPtr)(void);

      void FunctionA(void)
      {
      printf("You are in Function A.\n");
      }

      int main(void)
      {
      printf("Here is a program that does something.\n");
      VoidFunctionPtr fp = FunctionA; // FAILS because variables must be declared first.
      fp();
      }

      Revised code:

      #include typedef void (*VoidFunctionPtr)(void);

      void FunctionA(void)
      {
      printf("You are in Function A.\n");
      }

      int main(void)
      {
      VoidFunctionPtr fp = FunctionA; // MOVED variable declaration to first line.

      printf("Here is a program that does something.\\n");
      fp();
      

      }

      I'm sure this wouldn't happen in a C++ project, but I am using compiler settings to force Standard C compilation. I come from a C++ background but I'm trying to learn the differences between C++ and standard C. I bought a book on standard C, but it references the "C99" standard, which apparently allows variables to be defined anywhere in a function. I guess the MS C compiler works on an older standard?

      A Offline
      A Offline
      Albert Holguin
      wrote on last edited by
      #6

      When writing C, you should probably try to declare all variables at the beginning to make sure you have compatibility with all compilers since that wasn't allowed until later on (2000?). http://en.wikipedia.org/wiki/ANSI_C[^]

      X 1 Reply Last reply
      0
      • A Albert Holguin

        When writing C, you should probably try to declare all variables at the beginning to make sure you have compatibility with all compilers since that wasn't allowed until later on (2000?). http://en.wikipedia.org/wiki/ANSI_C[^]

        X Offline
        X Offline
        Xpnctoc
        wrote on last edited by
        #7

        Thanks for the tip.

        1 Reply Last reply
        0
        • X Xpnctoc

          I found the problem. I only posted a limited version of the code to focus the direction of the inquiry. In the context of the full code, what is going on is that the compiler expects all variables to be declared at the beginning of the function. Original code:

          #include typedef void (*VoidFunctionPtr)(void);

          void FunctionA(void)
          {
          printf("You are in Function A.\n");
          }

          int main(void)
          {
          printf("Here is a program that does something.\n");
          VoidFunctionPtr fp = FunctionA; // FAILS because variables must be declared first.
          fp();
          }

          Revised code:

          #include typedef void (*VoidFunctionPtr)(void);

          void FunctionA(void)
          {
          printf("You are in Function A.\n");
          }

          int main(void)
          {
          VoidFunctionPtr fp = FunctionA; // MOVED variable declaration to first line.

          printf("Here is a program that does something.\\n");
          fp();
          

          }

          I'm sure this wouldn't happen in a C++ project, but I am using compiler settings to force Standard C compilation. I come from a C++ background but I'm trying to learn the differences between C++ and standard C. I bought a book on standard C, but it references the "C99" standard, which apparently allows variables to be defined anywhere in a function. I guess the MS C compiler works on an older standard?

          M Offline
          M Offline
          Michael Dunn
          wrote on last edited by
          #8

          MS isn't concerned with keeping up with the latest C specs, so you'll need to avoid C99-specific features.

          --Mike-- Dunder-Mifflin, this is Pam.

          X 1 Reply Last reply
          0
          • M Michael Dunn

            MS isn't concerned with keeping up with the latest C specs, so you'll need to avoid C99-specific features.

            --Mike-- Dunder-Mifflin, this is Pam.

            X Offline
            X Offline
            Xpnctoc
            wrote on last edited by
            #9

            Yeah, I figured that out. Do you happen to know what (if any) standard they do conform to? I know there are several through the years.

            A 1 Reply Last reply
            0
            • X Xpnctoc

              Yeah, I figured that out. Do you happen to know what (if any) standard they do conform to? I know there are several through the years.

              A Offline
              A Offline
              Albert Holguin
              wrote on last edited by
              #10

              If you look at the link I provided before, it states that they implemented "Microsoft Visual C++ (C90. A few features of C99)". I'm guessing you would have to dig deep to figure out what they did and didn't implement.

              X 1 Reply Last reply
              0
              • X Xpnctoc

                I found the problem. I only posted a limited version of the code to focus the direction of the inquiry. In the context of the full code, what is going on is that the compiler expects all variables to be declared at the beginning of the function. Original code:

                #include typedef void (*VoidFunctionPtr)(void);

                void FunctionA(void)
                {
                printf("You are in Function A.\n");
                }

                int main(void)
                {
                printf("Here is a program that does something.\n");
                VoidFunctionPtr fp = FunctionA; // FAILS because variables must be declared first.
                fp();
                }

                Revised code:

                #include typedef void (*VoidFunctionPtr)(void);

                void FunctionA(void)
                {
                printf("You are in Function A.\n");
                }

                int main(void)
                {
                VoidFunctionPtr fp = FunctionA; // MOVED variable declaration to first line.

                printf("Here is a program that does something.\\n");
                fp();
                

                }

                I'm sure this wouldn't happen in a C++ project, but I am using compiler settings to force Standard C compilation. I come from a C++ background but I'm trying to learn the differences between C++ and standard C. I bought a book on standard C, but it references the "C99" standard, which apparently allows variables to be defined anywhere in a function. I guess the MS C compiler works on an older standard?

                A Offline
                A Offline
                Albert Holguin
                wrote on last edited by
                #11

                Another difference that's easy for C++ devs to forget is the fact that C doesn't support parameters with defaults. Example: Valid in C++, not valid in C

                int GetFoo(int HowMany=1){}

                X 1 Reply Last reply
                0
                • A Albert Holguin

                  If you look at the link I provided before, it states that they implemented "Microsoft Visual C++ (C90. A few features of C99)". I'm guessing you would have to dig deep to figure out what they did and didn't implement.

                  X Offline
                  X Offline
                  Xpnctoc
                  wrote on last edited by
                  #12

                  Oh, there it is. I don't know how I missed that before :-)

                  1 Reply Last reply
                  0
                  • A Albert Holguin

                    Another difference that's easy for C++ devs to forget is the fact that C doesn't support parameters with defaults. Example: Valid in C++, not valid in C

                    int GetFoo(int HowMany=1){}

                    X Offline
                    X Offline
                    Xpnctoc
                    wrote on last edited by
                    #13

                    That's OK by me. I don't recall ever using defaults... not since college 15 years ago anyway when some homework assignment was specifically targeted at it. I do a fair amount of work in C# too, which doesn't allow defaults, so I'm used to it.

                    A 1 Reply Last reply
                    0
                    • X Xpnctoc

                      That's OK by me. I don't recall ever using defaults... not since college 15 years ago anyway when some homework assignment was specifically targeted at it. I do a fair amount of work in C# too, which doesn't allow defaults, so I'm used to it.

                      A Offline
                      A Offline
                      Albert Holguin
                      wrote on last edited by
                      #14

                      I use C++ A LOT, so I do forget that one sometimes... and as you've noticed, depending on the compiler (and settings), it may or may not allow it... pain in the butt! X|

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups