screwed here!!!!!

This is a discussion on screwed here!!!!! within the C++ Programming forums, part of the General Programming Boards category; Code: int fun(int(*)()); int main() { fun(main); cout<<"in main"<<endl; return 0; } fun(int (*p)()) { cout<<"in fun"<<endl; return 0; } ...

  1. #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

  2. #2
    Registered User jdragyn's Avatar
    Join Date
    Sep 2009
    Posts
    93
    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;
    }

  3. #3
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    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

  4. #4
    Professional Chef leeor_net's Avatar
    Join Date
    Apr 2004
    Location
    Charles Town, WV
    Posts
    147
    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.
    Last edited by leeor_net; 11-11-2009 at 01:24 AM.
    - Leeor

  5. #5
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    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

  6. #6
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    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()

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,287
    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%.

  8. #8
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    Sorry, I'm not really a language lawyer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I screwed my pendrive
    By abh!shek in forum Tech Board
    Replies: 0
    Last Post: 06-30-2008, 01:32 PM
  2. Dev-C++ 4.9.9.2 is screwed up
    By jmd15 in forum C++ Programming
    Replies: 6
    Last Post: 09-30-2005, 05:27 AM
  3. i was told something and it screwed me up!
    By DarkViper in forum C++ Programming
    Replies: 5
    Last Post: 10-28-2002, 01:02 PM
  4. File I/O is screwed up bad?(or i am one)
    By no-one in forum C Programming
    Replies: 7
    Last Post: 03-09-2002, 08:04 PM
  5. ARG.... My Texture Loader is screwed... PLEASE HELP!
    By minime6696 in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 01-22-2002, 04:45 PM

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