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. C / C++ / MFC
  4. Pointer Casting warning

Pointer Casting warning

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
7 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.
  • S Offline
    S Offline
    Software2007
    wrote on last edited by
    #1

    Hi, while converting C code to C++, I get a casting warning that I can't get rid of "Warning (Anachronism): Assigning void(*)(int) to extern "C" void(*)(int)" Anyone? Thanks

    #include struct sigaction sgnl_action;

    void handle_alarm() {
    alarm_timededout_=TRUE;
    ...
    }
    //In C no warnings
    void alarm_manager_(char *typ) {
    if (!strcmp(typ,"init")) {
    void (*func)() = handle_alarm;
    sgnl_action.sa_handler = func;
    ....
    }

    //In C++
    void alarm_manager_(char *typ) {
    if (!strcmp(typ,"init")) {
    sgnl_action.sa_handler = (void (*)(int))handle_alarm; /////warning on this line
    ....
    }

    D K C T 4 Replies Last reply
    0
    • S Software2007

      Hi, while converting C code to C++, I get a casting warning that I can't get rid of "Warning (Anachronism): Assigning void(*)(int) to extern "C" void(*)(int)" Anyone? Thanks

      #include struct sigaction sgnl_action;

      void handle_alarm() {
      alarm_timededout_=TRUE;
      ...
      }
      //In C no warnings
      void alarm_manager_(char *typ) {
      if (!strcmp(typ,"init")) {
      void (*func)() = handle_alarm;
      sgnl_action.sa_handler = func;
      ....
      }

      //In C++
      void alarm_manager_(char *typ) {
      if (!strcmp(typ,"init")) {
      sgnl_action.sa_handler = (void (*)(int))handle_alarm; /////warning on this line
      ....
      }

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      What happens with:

      void handle_alarm( int )
      {
      }
      ...
      sgnl_action.sa_handler = handle_alarm;

      S 1 Reply Last reply
      0
      • D David Crow

        What happens with:

        void handle_alarm( int )
        {
        }
        ...
        sgnl_action.sa_handler = handle_alarm;

        S Offline
        S Offline
        Software2007
        wrote on last edited by
        #3

        I get the same exact warning, thats how I had it originally, then I explicitly specified the casting thinking it will get rid of the warning. It seems to get rid of the warning if I do this, but not sure if its correct functionality?

        extern "C" void print(char *)
        {
        ...
        }

        1 Reply Last reply
        0
        • S Software2007

          Hi, while converting C code to C++, I get a casting warning that I can't get rid of "Warning (Anachronism): Assigning void(*)(int) to extern "C" void(*)(int)" Anyone? Thanks

          #include struct sigaction sgnl_action;

          void handle_alarm() {
          alarm_timededout_=TRUE;
          ...
          }
          //In C no warnings
          void alarm_manager_(char *typ) {
          if (!strcmp(typ,"init")) {
          void (*func)() = handle_alarm;
          sgnl_action.sa_handler = func;
          ....
          }

          //In C++
          void alarm_manager_(char *typ) {
          if (!strcmp(typ,"init")) {
          sgnl_action.sa_handler = (void (*)(int))handle_alarm; /////warning on this line
          ....
          }

          K Offline
          K Offline
          krmed
          wrote on last edited by
          #4

          The parentheses you have in

          sgnl_action.sa_handler = (void (*)(int))handle_alarm; /////warning on this line

          looks strange to me. Perhaps

          sgnl_action.sa_handler = (void*)((int)handle_alarm); /////warning on this line

          would solve your problem. Just a guess at the moment - not near my VS to test it.

          Karl - WK5M PP-ASEL-IA (N43CS) PGP Key: 0xDB02E193 PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

          S 1 Reply Last reply
          0
          • K krmed

            The parentheses you have in

            sgnl_action.sa_handler = (void (*)(int))handle_alarm; /////warning on this line

            looks strange to me. Perhaps

            sgnl_action.sa_handler = (void*)((int)handle_alarm); /////warning on this line

            would solve your problem. Just a guess at the moment - not near my VS to test it.

            Karl - WK5M PP-ASEL-IA (N43CS) PGP Key: 0xDB02E193 PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

            S Offline
            S Offline
            Software2007
            wrote on last edited by
            #5

            This would give an error because the cast is (void(*)(int))(func). Anyways, the complain is about tryiing to cast to an extern "C", when I change my function to an extern "C", it seems to work fine. So, it looks as the signal handler functions have to be standalone external C functions. Thanks

            1 Reply Last reply
            0
            • S Software2007

              Hi, while converting C code to C++, I get a casting warning that I can't get rid of "Warning (Anachronism): Assigning void(*)(int) to extern "C" void(*)(int)" Anyone? Thanks

              #include struct sigaction sgnl_action;

              void handle_alarm() {
              alarm_timededout_=TRUE;
              ...
              }
              //In C no warnings
              void alarm_manager_(char *typ) {
              if (!strcmp(typ,"init")) {
              void (*func)() = handle_alarm;
              sgnl_action.sa_handler = func;
              ....
              }

              //In C++
              void alarm_manager_(char *typ) {
              if (!strcmp(typ,"init")) {
              sgnl_action.sa_handler = (void (*)(int))handle_alarm; /////warning on this line
              ....
              }

              C Offline
              C Offline
              Chuck OToole
              wrote on last edited by
              #6

              Because the unix / posix libraries work with either C or C++, they all declare their external functions as 'extern "C"' to get the C calling conventions. This is done in the header files, in your case, Signal.h

              #ifdef __cplusplus
              extern "C" {
              #endif

              The field sa_handler needs to point to a function that is declared as "C" callable. Use __cdecl. Note that C calling conventions also affect how the stack arguments are cleaned up so unless you declare the action function correctly, you will end up with stack corruption problems later.

              void __cdecl handle_alarm() {
              alarm_timededout_=TRUE;
              ...
              }

              1 Reply Last reply
              0
              • S Software2007

                Hi, while converting C code to C++, I get a casting warning that I can't get rid of "Warning (Anachronism): Assigning void(*)(int) to extern "C" void(*)(int)" Anyone? Thanks

                #include struct sigaction sgnl_action;

                void handle_alarm() {
                alarm_timededout_=TRUE;
                ...
                }
                //In C no warnings
                void alarm_manager_(char *typ) {
                if (!strcmp(typ,"init")) {
                void (*func)() = handle_alarm;
                sgnl_action.sa_handler = func;
                ....
                }

                //In C++
                void alarm_manager_(char *typ) {
                if (!strcmp(typ,"init")) {
                sgnl_action.sa_handler = (void (*)(int))handle_alarm; /////warning on this line
                ....
                }

                T Offline
                T Offline
                tushar_patil
                wrote on last edited by
                #7

                hi I dnt knw clearly what type of

                sgnl_action.sa_handler

                is. But this is a little piece of code without any warning and errors might be helpful to you.

                #include <stdio.h>

                void (*sa_handler)(int); // function pointer takes int type
                // arg and retuns void as we have to
                //typecast func handle_alarm to this type

                void handle_alarm() {
                printf("helooo");
                }

                void main(int argc, _TCHAR* argv[])
                {
                handle_alarm(); //normal call to function
                sa_handler = (void (*)(int))handle_alarm; // compatible typecast
                sa_handler(0); // call with a function pointer
                // as it takes one argument we have
                //to pass a dummy arg.
                }

                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