Thread: Passing structure to function

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    30

    Passing structure to function

    The following code fragment comes from Schildt's 4th Edition Complete C Reference:
    Code:
    /* Example of passing a structure pointer to a function */
    /* Extraneous code deleted */
    
    struct example {
        int member;
    };
    
    void function_example(struct example *ptr);
    
    main (void)
    {
        struct example passthis;
    
        function_example(&passthis);
    }
    
    void function_example(struct example *ptr)
    {
        ptr -> member = 0;
    }
    My question is: Why is (&passthis) passed to the function instead of first initializing (ptr) to (&passthis) and then passing (ptr)? If that were done, couldn't the function header be
    Code:
    function_example(*ptr)
    ?

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    No matter how you pass it (via &<variable> or a pointer) you still have to specify what type of pointer to accept in function_example.

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    30
    OK, thanks.

  4. #4
    Quote Originally Posted by Sereby
    The following code fragment comes from Schildt's 4th Edition Complete C Reference:
    Code:
    /* Example of passing a structure pointer to a function */
    /* Extraneous code deleted */
    
    struct example {
        int member;
    };
    
    void function_example(struct example *ptr);
    
    main (void)
    {
        struct example passthis;
    
        function_example(&passthis);
    }
    
    void function_example(struct example *ptr)
    {
        ptr -> member = 0;
    }
    Of course, it's Schildt's code, hence it's wrong, as usual..
    Code:
    int main (void)
    {
        struct example passthis;
    
        function_example(&passthis);
       return 0;
    }
    My question is: Why is (&passthis) passed to the function instead of first initializing (ptr) to (&passthis) and then passing (ptr)?
    It is exactly 'what function_example(&passthis)' does. There is no simpler or more straightforward way to do it.
    If that were done, couldn't the function header be
    Code:
    function_example(*ptr)
    Certainely not. Are you trying to rewrite the specifications of the language or what ?
    Emmanuel Delahaye

    "C is a sharp tool"

  5. #5
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Quote Originally Posted by Sereby
    The following code fragment comes from Schildt's 4th Edition Complete C Reference:
    Code:
    /* Example of passing a structure pointer to a function */
    /* Extraneous code deleted */
    
    struct example {
        int member;
    };
    
    void function_example(struct example *ptr);
    
    main (void)
    {
        struct example passthis;
    
        function_example(&passthis);
    }
    
    void function_example(struct example *ptr)
    {
        ptr -> member = 0;
    }
    My question is: Why is (&passthis) passed to the function instead of first initializing (ptr) to (&passthis) and then passing (ptr)? If that were done, couldn't the function header be
    Code:
    function_example(*ptr)
    ?

    You should do a seach for schildt on the board.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing a byte and its bits to a function
    By rtarbell in forum C Programming
    Replies: 9
    Last Post: 12-04-2008, 09:24 AM
  2. passing pointer to a structure in a function
    By steve1_rm in forum C Programming
    Replies: 5
    Last Post: 02-03-2008, 02:48 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM