Function Pointers
In modem code, depending on the parameters different function needs to be called.
Ex
For SMS , CALL , USSD , INTERNET , LOCATION different function needs to be called.
We can't have one to one mapping from up to down for these functionality s.
So when at UI your press any one of the options. There will a common function whose work will be to post or call respective functions.
We use function pointers mostly in those cases.
Ex , Check the below two codes
typedef enum{
call =0,
sms,
ussd,
location
}request_type;
switch(request_type)
case (call) :
call_fucntion();
case(sms):
sms_function();
case(ussd)
ussd_function();
The other way is
typedef void (*func_dummy)(void);
func_dummy functions[4];
functions[call] = call_function;
fucntions[sms] = sms_function;
Now rather than having switch statement with lot of cases, we can have simply functions pointer , in which index will be request.
so functions(request_type) will call the correct functions. These also helps in debugging, since rather than checking
1) enums
2) switch statement, also break after each case
3) whether correct function is mapped or not
4) having user readable names of cases an function names
In case of function pointers we have to worry only about 3 part.
There are mostly two ways of defining function pointer
typedef void(*func_dummy)(void);
func_dummy functions[8];
typedef void(func_dummy)(void)
func_dummy* functions[8]; // Note we have use pointer here
Since
func_dummy functions[8]; // This will throw error saying function array can't be declared.
Not sure why , but I think this is due to function is always accessible by address.
Ex :
int a;
here a and &a are different, one is value and other is address of a
but in case of function
void hello()
{
}
hello or &hello both are same , implicitly hello is same as &hello.
So when you declare array of functions, they is no way of assigning values to it . So we have to always define array of function pointers.
.
In modem code, depending on the parameters different function needs to be called.
Ex
For SMS , CALL , USSD , INTERNET , LOCATION different function needs to be called.
We can't have one to one mapping from up to down for these functionality s.
So when at UI your press any one of the options. There will a common function whose work will be to post or call respective functions.
We use function pointers mostly in those cases.
Ex , Check the below two codes
typedef enum{
call =0,
sms,
ussd,
location
}request_type;
switch(request_type)
case (call) :
call_fucntion();
case(sms):
sms_function();
case(ussd)
ussd_function();
The other way is
typedef void (*func_dummy)(void);
func_dummy functions[4];
functions[call] = call_function;
fucntions[sms] = sms_function;
Now rather than having switch statement with lot of cases, we can have simply functions pointer , in which index will be request.
so functions(request_type) will call the correct functions. These also helps in debugging, since rather than checking
1) enums
2) switch statement, also break after each case
3) whether correct function is mapped or not
4) having user readable names of cases an function names
In case of function pointers we have to worry only about 3 part.
There are mostly two ways of defining function pointer
typedef void(*func_dummy)(void);
func_dummy functions[8];
typedef void(func_dummy)(void)
func_dummy* functions[8]; // Note we have use pointer here
Since
func_dummy functions[8]; // This will throw error saying function array can't be declared.
Not sure why , but I think this is due to function is always accessible by address.
Ex :
int a;
here a and &a are different, one is value and other is address of a
but in case of function
void hello()
{
}
hello or &hello both are same , implicitly hello is same as &hello.
So when you declare array of functions, they is no way of assigning values to it . So we have to always define array of function pointers.
.
No comments:
Post a Comment