CallFunc !


 
Thread Tools Search this Thread
Top Forums Programming CallFunc !
# 1  
Old 01-18-2006
Network CallFunc !

Smilie Hi All
I have a littel problem with this code
When I declare struct
typedef struct
( int a;
int * (CallFunc)(int a,int b);
) d;

and when using d.CallFunc
I can't understand what this means
and how CallFunc work
pls help me
thank you so muc
# 2  
Old 01-19-2006
imagine the struct as a skeleton .
it has 2 placeholders(say) :

int a;
int * (CallFunc)(int a,int b);
which needs to b filled by appropriate values.


now the call
d.CallFunc
fill (or) calls(in this case) the second fn of the struct to use it for storing
values.
# 3  
Old 01-19-2006
CallFunc is a function pointer - it takes two arguments, and return a pointer to an int.
# 4  
Old 01-19-2006
Once the structure is properly initialised ,

(d.CallFunc)() will dereference to the function to which CallFunc pointer is pointing , and invoke it I guess. Meaning it will work like a normal function call. That is what my guess is.

~Jackal
# 5  
Old 01-19-2006
int * (CallFunc)(int a,int b);
is not a function pointer, it is just a function that returns a pointer to an int.

however, int (*CallFunc)(int a, int b) is a function pointer meaning any function that returns an int and takes 2 integers as an argument can be pointed by CallFunc.

for eg, if you have int fn(int, int), then CallFunc = fn is valid statement and calling (CallFunc)(int) will actually result in execution of fn. Further down if you assign CallFunc = fn2 where fn2 is of type int fn2(int, int), and then you call (CallFunc)(int), you actually call fn2.

going further ahead you can have an array of this pointer and make its individual element point to varying functions.

check http://www.geocities.com/hemantborole/
click on KnowledgeBank, C (under my programs), and in the list see the fnptr.c and func_ptr.c programs.
they are not pointers in structure, but they are function pointers and you can happily put them as a member of a structure.
# 6  
Old 01-20-2006
int * (CallFunc)(int a,int b);
is not a function pointer, it is just a function that returns a pointer to an int.

The above statement given by linuxpenguin is exactly right. As it was said, CallFunc is just a functionname and this function returns an integer pointer.


The example given by linuxpenguin explains very well.
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question