13 Replies Latest reply on Sep 4, 2014 5:16 PM by ki.leung

    Function Pointer in Referenced Struct

    peter.taylor

       I certainly may be missing something obvious, but there seems to be an issue with implementing a function pointer in a struct referenced by a pointer:

         

      struct MyStruct {

         

      void (*SetDriveMode)(uint8);

         

      };

         

       

         

      void Function1(const struct MyStruct * structPntr) {

         

      void (*foo)(uint8);

         

      foo = PIN1_SetDriveMode;

         

      structPntr->SetDriveMode(PIN_DM_STRONG);

         

      }

         

       

         

      struct MyStruct structVar;

         

      main {

         

      structVar.MyFunction = PIN1_SetDriveMode;

         

      Function1(&structVar)

         

      }

         

       

         

      Notice that foo is not actually used at all.  If I compile my program written like above, it appears to work properly.  I can place a breakpoint at the "structPntr->SetDriveMode(PIN_DM_STRONG);" line and it shows me the correct function address both in foo and in the SetDriveMode function pointer.  However, if I just comment out the "foo = PIN1_SetDriveMode;" line and recompile, it will not work properly and when the debugger halts at the breakpoint, the member values of *structPntr are blank.  I would greatly appreciate any thoughts y'all might have.  Thanks.