pavikirthi
pavikirthi

Reputation: 1655

passing function pointer in c with a pointer argument

Here, I have defined two function pointers func1 and func2, and I guess this a way to use function pointers in c.But my question is when I call

executor(func1);
executor(func2);

How can I allocate memory for uint8_t * and pass it as an argument for either func1 and func2, so that I can use it back in main()

typedef int (*FUNC_PTR)(uint64_t, uint8_t*);

void executor(FUNC_PTR func)
{ 
   uint64_t opts = 0x0000010;
   uint8_t val = 0;
   res = func(opts, &val);
}

int func1(uint64_t a, uint8_t* b)
{ 
   //do some stuff

}

int func2(uint64_t a, uint8_t* b)
{ 
//do some stuff
}

void main()
{
   executor(func1);
   executor(func2);
}

Upvotes: 0

Views: 43

Answers (1)

merlin2011
merlin2011

Reputation: 75629

Just add it to the argument list of executor.

void executor(FUNC_PTR func, uint8_t* b)
{ 
   uint64_t opts = 0x0000010;
   res = func(opts, b);
}

You can allocate the memory on the stack or heap in main.

void main()
{
   uint8_t mem1, mem2;
   executor(func1, &mem1);
   executor(func2, &mem2);
}

Upvotes: 1

Related Questions