How to Return Function Pointer's Memory Address?

B

Bryan Parkoff

I set Function Pointer variable to private so unauthorized users cannot
access it, but they are allowed to use Get_ and Set_ functions to modify
Function Pointer variable. Look at my example.

unsigned char foo(void)
{
return 0xC1;
}

unsigned char (*pfoo)(void) = foo;

unsigned char* Get_foo(void)
{
// return pfoo; // Error -- can't return function pointer
return unsigned char (*pfoo)(void); // Error -- can't return function
pointer
}

Can you please correct Get_foo() function?

Bryan Parkoff
 
B

Ben

Bryan said:
I set Function Pointer variable to private so unauthorized users cannot
access it, but they are allowed to use Get_ and Set_ functions to modify
Function Pointer variable. Look at my example.

unsigned char foo(void)
{
return 0xC1;
}

unsigned char (*pfoo)(void) = foo;

unsigned char* Get_foo(void)
{
// return pfoo; // Error -- can't return function pointer
return unsigned char (*pfoo)(void); // Error -- can't return function
pointer
}

Can you please correct Get_foo() function?

Take a look here:

http://www.newty.de/fpt/fpt.html#r_value

Ben
 
G

Gabriel

Bryan said:
I set Function Pointer variable to private so unauthorized users cannot
access it, but they are allowed to use Get_ and Set_ functions to modify
Function Pointer variable. Look at my example.

unsigned char foo(void)

Bad habit.
unsigned char foo()
{
return 0xC1;
}

unsigned char (*pfoo)(void) = foo;

unsigned char(*pfoo)() = foo;
unsigned char* Get_foo(void)

unsigned char(*)() Get_Foo()
{
// return pfoo; // Error -- can't return function pointer
return unsigned char (*pfoo)(void); // Error -- can't return function
pointer
}

Can you please correct Get_foo() function?
Sure.

The above works. But it is easier to write:
unsigned char foo()
{
return 0xC1;
}

typedef unsigned char (*pfoo_type)();

pfoo_type pfoo = foo;

pfoo_type Get_Foo()
{
return pfoo;
}

Gabriel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top