C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-11-2009, 01:37 AM   #1
Registered User
 
Join Date: Oct 2009
Posts: 11
Question screwed here!!!!!

Code:
int fun(int(*)());

int main()
{
   fun(main);
   cout<<"in main"<<endl;
   return 0;
}

fun(int (*p)())
{
  cout<<"in fun"<<endl;
  return 0;
}
the output is....

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   Reply With Quote
Old 11-11-2009, 01:52 AM   #2
Registered User
 
jdragyn's Avatar
 
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   Reply With Quote
Old 11-11-2009, 02:14 AM   #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   Reply With Quote
Old 11-11-2009, 02:19 AM   #4
Professional Chef
 
leeor_net's Avatar
 
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   Reply With Quote
Old 11-11-2009, 02:34 AM   #5
Registered User
 
Join Date: Oct 2009
Location: While(1)
Posts: 368
Quote:
Originally Posted by leeor_net View Post
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.

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   Reply With Quote
Old 11-11-2009, 08:43 AM   #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)()
means.

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   Reply With Quote
Old 11-11-2009, 10:35 AM   #7
Registered User
 
Join Date: Jun 2005
Posts: 1,439
Quote:
Originally Posted by rags_to_riches View Post
the name of a function, without any brackets(), is a pointer to the function with that name.
Not true. The name of a function is not a pointer. It can be converted by the compiler into a pointer to a function in some contexts.

Quote:
Originally Posted by rags_to_riches View Post
Hence, main is a pointer to the function
Code:
int main()
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   Reply With Quote
Old 11-11-2009, 11:31 AM   #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   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 04:59 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22