Hi,
I have tried to simplyfy to read and write the function pointer with some self developed rule. Just apply this rule.It may be coincidence with others.....But i have tried to make it easy to read and understan....:)In previous example i just tried to use it , but this time i tried to explain it. I would welcome, if someone suggest a batter way that must be an easy way of reading and writing it.
.....................................................................................
Function Pointers:Here is an example of a function pointer.
Void (*fPtr)(void );
Here, fPtr is a pointer to a function, which takes nothing and returns nothing.
Here we will practice reading of some expression.
Like
int * p; How do we read the above declaration?
Rule is…. 1)
The answer is to start with from the name of the variable, here it is p.
Rule 2) Go to right then come back Rule 3) then go left. And finish the reading always.
So here start with variable p.
P is a….R 2 Go right …. Nothing is there , just leave
R 3 go left of the variable P i.e *
Pointer toGo right nothing is there , just leave ,
Go left, int is there so… read
int
If you add together all the readings then it should be like this
P is a pointer to integer. Is this not a easy way of remembering? It will help us to read function pointer. Just go right , just finish reading, then go left… do this practice, you will be end of with reading the whole function pointer, no need to by heart the expression.
Now we will take an example of function pointer.
Example 1
Void (*fPtr)(void );
Lets use our formula here, and as I told , start with the variable name. here it is fPtr.
Step 1) rule 1 …What is there to the right of fPtr? Ans= Nothing. So leave it.
Step 2, apply rule 2... What is there to the left of fPtr? Ans = * and with this our reading finished within the parenthesis. Am I correct? Yes…..
So now we got
;-
fPtr is a pointer to
Boss reading finished, just come out parenthesis,
What is there to the right of parenthesis? Ans = (void) which is nothing but function taking no argument (void) …..Cool :)
So now we got
Function taking no argument….
Reading finished, go to left … What is there to left of parenthesis? Ans = void, which is a return type of function… Am I right? Yes Boss….. Cool :)
So now we go
Returning void
Now we will adding all that we got from reading right and left of the starting variable name fPtr.
“fPtr is a pointer to Function taking no argument Returning void “
How easy it is …..:) Is not it?
I have done one example for you .Now Big Boss you have to help me to read one more for me.
Example 2
Int myArr[3])(int);
Boss it is easy, just apply my formula, then you will feel, it is dam easy.
:)
:)
What I have to tell? Ok we both will help each other…cool :)
1) Lets start with variable name ,i.e myArr. Ok…
2) Go right , we find array notation of 3 ok…
3) So we got
4)
“myArr is an array of 3” 5) Go left ,we find “*” ,
6) So we got
7)
“ Pointers to “ 8) Come out parentheses ,because nothing is left there.
9) Go to the right of parentheses ,we find “(int) which is indicator of function.
10) So we got
11)
“Function taking one argument as integer”
12) Go left , here it returns an integer.
13) So we got
14) “returns an integer” 15) Just add together what we got from our reading by using our formula.
16)
myArr is an array of 3 Pointers to Function taking one argument as integer and returns an integer Ha ha ha hi hi hi hu hu hu… cool.
Do we need an example program for this..? After lunch we need some sweets. Kuch mitha ho jaye.. :)
#include
int show1(int a)
{
printf("\n show1 a=%d",a);
return 1;
}
int show2(int c)
{
printf("\n show2 c=%d",c);
return 2;
}
int main()
{
int (*myArr[3])(int);
myArr[0] = show1;
myArr[1] = show2;
myArr[0](10);
myArr[1](20);
return 0;
}
From Sahu
hara.sahu@gmail.com