Function Pointer in Referenced Struct

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

 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.

0 Likes
13 Replies