passing structs & pointers to structs as arguments

This is a discussion on passing structs & pointers to structs as arguments within the C Programming forums, part of the General Programming Boards category; I'm writing a whole set of functions that needs to edit a huge data structure, and I'm getting a little ...

  1. #1
    Registered User Markallen85's Avatar
    Join Date
    Nov 2002
    Posts
    53

    passing structs & pointers to structs as arguments

    I'm writing a whole set of functions that needs to edit a huge data structure, and I'm getting a little confused over exactly how some things work:

    1) I know things like arrays are passed to functions as pointers, does a struct work the same way? eg if I pass a struct to a function & edit it, do those changes remain after the function terminates?

    2)if I want to be able to edit a pointer to a struct (eg free & malloc it to somewhere else) inside a function, then by my understanding I have to pass the function a pointer to a pointer to a struct. this leads to the clunky syntax:

    (*struct)->member;

    is there any way to reduce the number of pointers to pointers involved here?


    thanks
    -mark
    "never argue with an idiot, they will drag you down to their level and beat you with experience"

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1) Yes.
    Code:
    struct foo {
        int bar;
    };
    
    void function( struct foo *ptr )
    {
        ptr->bar = 5;
    }
    
    int main( void )
    {
        struct foo instance;
    
        function( &instance );
        printf("instance->bar = %d\n", instance->bar );
        return 0;
    }
    2) I'm not sure what you mean exactly. Perhaps...
    Code:
    struct foo {
        int bar;
    };
    
    void function( struct foo **ptrptr )
    {
        struct foo *ptr;
    
        ptr = *ptrptr;
        ...do all you need using ptr...
    
        ....update ptrptr when finished...
    }
    Something like that should suffice.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User Markallen85's Avatar
    Join Date
    Nov 2002
    Posts
    53
    1)not quite what I meant, I know that the code you've given will work fine, I was wondering about something slightly different. Anyway I just tested it myself & it don't work, so nevermind.

    2)thanks quzah. a temp pointer (*ptrptr) does clean things up a fair bit..... I've used temp pointers all over the rest of the program, for some reason the idea simply didn't hit me to use it here, till now.


    thanks again
    -mark
    "never argue with an idiot, they will drag you down to their level and beat you with experience"

  4. #4
    Registered User
    Join Date
    Feb 2004
    Posts
    72

    1) I know things like arrays are passed to functions as pointers, does a struct work the same way? eg if I pass a struct to a function & edit it, do those changes remain after the function terminates?
    Originally posted by quzah
    1) Yes.
    Wrong. structs are NOT passed to functions as pointers, they are copied. Any changes made to the structure only change the copy and they will be lost once the function terminates.

    Code:
    #include <stdio.h>
    
    struct foo {
        int bar;
    };
    
    void function( struct foo instance )
    {
        instance.bar = 5;
    }
    
    int main( void )
    {
        struct foo instance;
        instance.bar = 1;
        printf("instance.bar = %d\n", instance.bar); /* 1 */
        function( instance );
        printf("instance.bar = %d\n", instance.bar ); /* still 1 */
        return 0;
    }
    If you want to edit a structure in a function either pass in a pointer to a structure (explicitly) or return the structure.
    Last edited by major_blagger; 03-16-2004 at 12:29 PM.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by major_blagger
    Wrong. structs are NOT passed to functions as pointers, they are copied. Any changes made to the structure only change the copy and they will be lost once the function terminates.
    You took a literal translation. I provided a way to do what they wanted to.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Feb 2004
    Posts
    72
    Originally posted by quzah
    You took a literal translation. I provided a way to do what they wanted to.
    Quzah.
    I took the question he asked. You answered what he didn't ask and already knew.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by major_blagger
    I took the question he asked. You answered what he didn't ask and already knew.
    I misread. I thought he was saying "Is there a way to make it so you can update a structure when passed to a function?".

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. parameter passing and pointers
    By pokiepo in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 01:13 AM
  2. Passing arguments between functions
    By Wiretron in forum C Programming
    Replies: 4
    Last Post: 05-17-2006, 05:59 PM
  3. Pointers, structs, and functions, oh my!
    By funkydude9 in forum C++ Programming
    Replies: 8
    Last Post: 07-30-2003, 01:51 AM
  4. array of pointers to structs
    By stumon in forum C Programming
    Replies: 7
    Last Post: 03-24-2003, 07:13 AM
  5. structs like pointers?
    By mart_man00 in forum C Programming
    Replies: 5
    Last Post: 03-14-2003, 03:16 AM

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