-5

I am trying to write function for Initialise system while inserting a function pointer for System triggered handling with System index and the value So How can I access the variables index and val in System_Init function

typedef void (*System_Handler)(unsigned int short indx, char val);

void System_Init(Switch_Handler sw_hdl)
{
  unsigned short int test; 
  test = indx;
  /* Need to access variables indx and val  Here . How can we do ?*/
}
Zeta
  • 103,620
  • 13
  • 194
  • 236
  • Possible duplicate of [How do function pointers in C work?](https://stackoverflow.com/questions/840501/how-do-function-pointers-in-c-work) – ventaquil May 22 '19 at 14:07
  • 3
    In `System_Init` I do not see anything of `System_Handler`. So what do you mean? – Paul Ogilvie May 22 '19 at 14:07
  • Please read this: [ask] and show a [mcve] – Jabberwocky May 22 '19 at 14:08
  • Is the System_Init argument supposed to be a System_Handler not a Switch_Handler? (And a pointer type?) And if you're passing in a function pointer then I'm not sure what you're trying to do in accessing its arguments: the point is you can call the pointer, you're not implementing the function parameter there. – Rup May 22 '19 at 14:08

1 Answers1

3

Function pointers don't come bundled with a set of function arguments. You have to provide the arguments separately, usually by passing them alongside with the function pointer:

typedef void (*System_Handler)(unsigned int short indx, char val);

void System_Init(System_Handler sw_hdl, unsigned short indx, char val)
{
  unsigned short int test; 
  test = indx;
  //...
  sw_hdl(indx,val);
}
Petr Skocik
  • 58,047
  • 6
  • 95
  • 142