Fatpanda
Fatpanda

Reputation: 61

What is the correct way to assign function to a function pointer of a structure member

Can anyone please help me with how to pass a function to a function pointer inside struct in C? I have a function as below

typedef struct LOAD_DRV_DATA
{
   uint8_t LoadDrvId;
   uint8_t dataLength;
   uint8_t DataBuffer[8];
}LOAD_DRV_DATA;

typedef struct LOAD_DRV_MAP
{
    uint8_t rspRemoteNum;
    uint8_t loadDrvID;
    uint8_t loadDrvComIf;
    void (*loadDrvFnPtr)(void);
}LOAD_DRV_MAP;
        
void LoadDrv_writeConfigData(LOAD_DRV_DATA *configData)
{
   uint8_t chipSel = 0x00;
   uint8_t buff    = 0xFF;
   LOAD_DRV_MAP configLoadDrvMap;
   
   configLoadDrvMap.rspRemoteNum = configData->LoadDrvId;
   configLoadDrvMap.loadDrvFnPtr = &write_SpiConfig(configLoadDrvMap.rspRemoteNum, chipSel, buff);
}

I have a structure member called loadDrvFnPtr to which I have to assign the function. This has to be void because more functions will be assigned to this with different arguments so that the basic data type kept is void.

Upvotes: 2

Views: 104

Answers (2)

tofro
tofro

Reputation: 6073

There's simply too much wrong to answer this question with proper code:

You didn't use the structure in your assignment at all. It should look something like

  configLoadDrvMap.loadDrvFnPtr = write_SpiConfig;

This might, however, need a type cast depending on what exactly write_SpiConfigis and is likely wrong anyways because your function pointer in the structure expects a void (*)(void) and you are assigning a pointer to a non-void function that expects arguments.

Also, your use of arguments in the function pointer assignment hints you are understanding this as a function call - it isn't.

Upvotes: 1

Lundin
Lundin

Reputation: 214770

You have to separate the function pointer assignment and the function call.

It should just be configLoadDrvMap.loadDrvFnPtr = &write_SpiConfig;. In your code you are attempting to call the function at the same line as you assign the function pointer and that's not going to work - you'd end up assigning the function pointer to the result of the function call and the compiler will protest with some error message about incompatible types.


Best practices:

  • Using function pointers without typedef is bad style, since they quickly get hard to read.

  • Use a function pointer syntax similar to object pointer syntax for style consistency.

  • As a rule of thumb: good variable names explain what the variable does, not what the variable is.

    Variable names containing too much info about what type that variable has (ie FnPtr) is an indication of muddy design and lead to needless clutter. Programmers are expected to always look up the type of a variable before using it.

Example of how to fix the above:

typedef void load_drv_t (void); // function type

...

typedef struct LOAD_DRV_MAP
{
  ...
  load_drv_t* load_drv; // pointer to load_drv_t, quite self-documenting
}LOAD_DRV_MAP;

...

configLoadDrvMap.load_drv = &write_SpiConfig;

Upvotes: 2

Related Questions