![]() |
| | #1 |
| Registered User Join Date: Oct 2009
Posts: 11
| Code: int fun(int(*)());
int main()
{
fun(main);
cout<<"in main"<<endl;
return 0;
}
fun(int (*p)())
{
cout<<"in fun"<<endl;
return 0;
}
in fun in main how and in which manner is "main" being passed in the fun function argument in main function. does this way of passing automatically mean that a function pointer that returns an integer is being passed into the fun function argument???? how does this program work.. kindly help.. Thnx |
| ayan_2587 is offline | |
| | #2 |
| Registered User Join Date: Sep 2009
Posts: 45
| What are you expecting? You would get the same output if you wrote Code: int fun(int);
int main()
{
fun(42);
cout<<"in main"<<endl;
return 0;
}
fun(int x)
{
cout<<"in fun"<<endl;
return 0;
}
|
| jdragyn is offline | |
| | #3 |
| Registered User Join Date: Oct 2009 Location: While(1)
Posts: 368
| This is as simple as calling a normal function.. with the code above it will call fun and pass a function pointer with which you are not doing anything . Than it will call main cout |
| RockyMarrone is offline | |
| | #4 |
| Professional Chef Join Date: Apr 2004 Location: Charles Town, WV
Posts: 144
| Exactly what are you trying to do with your code? I've never seen a recursive call to an entry point function (main) let a lone a question that doesn't really ask anything. If you're expecting to see "in main" print before "in fun" than you've misunderstood function pointers and function calls altogether. You'd need to call main via your function pointer and, if you tried to do that with this program you'd end up in an infinte loop if it even worked at all. In this case, as you've not called main() via your function pointer your function fun() simply prints out a statement and then returns where main continues merily along.
__________________ - Leeor Last edited by leeor_net; 11-11-2009 at 02:24 AM. |
| leeor_net is offline | |
| | #5 | |
| Registered User Join Date: Oct 2009 Location: While(1)
Posts: 368
| Quote:
Hey dude what are you talkin abt i don't think so that he is recursively calling the main function he is just passing the pointer to the main function just not doing any thing with that pointer | |
| RockyMarrone is offline | |
| | #6 |
| and the Hat of Ass Join Date: Dec 2007
Posts: 813
| fun is defined as taking as its argument a pointer to a function which takes no arguments and returns an integer...that's what this Code: int (*p)() the name of a function, without any brackets(), is a pointer to the function with that name. Hence, main is a pointer to the function Code: int main() |
| rags_to_riches is offline | |
| | #7 | |
| Registered User Join Date: Jun 2005
Posts: 1,439
| Quote:
main() is a very special case in C++ - a pointer to main() cannot be created. This is related to the fact that main() cannot be called recursively.
__________________ Right 98% of the time, and don't care about the other 3%. | |
| grumpy is offline | |
| | #8 |
| and the Hat of Ass Join Date: Dec 2007
Posts: 813
| Sorry, I'm not really a language lawyer. |
| rags_to_riches is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| I screwed my pendrive | abh!shek | Tech Board | 0 | 06-30-2008 01:32 PM |
| Dev-C++ 4.9.9.2 is screwed up | jmd15 | C++ Programming | 6 | 09-30-2005 05:27 AM |
| i was told something and it screwed me up! | DarkViper | C++ Programming | 5 | 10-28-2002 02:02 PM |
| File I/O is screwed up bad?(or i am one) | no-one | C Programming | 7 | 03-09-2002 09:04 PM |
| ARG.... My Texture Loader is screwed... PLEASE HELP! | minime6696 | A Brief History of Cprogramming.com | 6 | 01-22-2002 05:45 PM |