Pointer as function argumnet

This is a discussion on Pointer as function argumnet within the C Programming forums, part of the General Programming Boards category; Hi, I have a code snippet: Code: #include<stdio.h> int fun(int(*) ()); int main() { fun(main); printf("Hi\n"); return 0; } int ...

  1. #1
    NKP
    NKP is offline
    Registered User
    Join Date
    Jan 2010
    Posts
    8

    Pointer as function argumnet

    Hi,
    I have a code snippet:

    Code:
    #include<stdio.h>
    int fun(int(*) ());
    int main()
    {
    fun(main);
    printf("Hi\n");
    return 0;
    }
    
    int fun(int (*p) ())
    {
    printf("Hello");
    return 0;
    }
    The output is
    Hello Hi

    Not clear what happens when fun(main) is invoked and why?
    Can anyone clarify?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You are passing an object of type
    Code:
    int(*) ()
    to the function; i.e., a function returning int and taking unknown arguments. In your function, it is called p.

    Then when you get to the function, you completely ignore it and do a printf. return returns from the function back to whence it came.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by tabstop View Post
    a function returning int and taking unknown arguments. In your function, it is called p.
    Then when you get to the function, you completely ignore it
    To really understand this you might want to try adding the following couple lines:

    Code:
    int fun(int (*p) ())
    {
    int x;
    printf("Hello");
    x = p();
    return 0;
    }
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    lol, and cause some sort of stack overflow?

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by DeadPlanet View Post
    lol, and cause some sort of stack overflow?
    Takes a hundred thousand iterations or so.

    Hello130833 Hello130834 Hello130835 Hello130836 Hello130837 Hello130838 Hello130839 Hello130840 Hello130841 Hello130842 Hello130843 Hello130844 Hello130845 Hello130846 Hello130847 Hello130848 Hello130849 Hello130850 Hello130851 Hello130852 Hello130853 Hello130854 Hello130855 Hello130856 Hello130857 Hello130858 HeSegmentation fault
    Clearly this is the helium variety fault.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 05:23 AM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  3. Function Pointer help
    By Skydt in forum C Programming
    Replies: 5
    Last Post: 12-02-2005, 09:13 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM

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