Passing structure to function

This is a discussion on Passing structure to function within the C Programming forums, part of the General Programming Boards category; The following code fragment comes from Schildt's 4th Edition Complete C Reference: Code: /* Example of passing a structure pointer ...

  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
    Helper
    Join Date
    Jun 2004
    Posts
    255
    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.

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-17-2006, 12:35 AM
  4. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 09:56 AM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM

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