Like Tree1Likes
  • 1 Post By javaeyes

passing struct by ref that was passed by ref from one function to another

This is a discussion on passing struct by ref that was passed by ref from one function to another within the C Programming forums, part of the General Programming Boards category; I have a struct which I passed to a function by reference. I now need to pass it again to ...

  1. #1
    Registered User javaeyes's Avatar
    Join Date
    Feb 2012
    Posts
    153

    passing struct by ref that was passed by ref from one function to another

    I have a struct which I passed to a function by reference. I now need to pass it again to another helper function. Do I need to pass it again by reference, or can I just pass it like a 'normal' variable, by value and derefence it. I think I can just pass it normally. Thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    Quote Originally Posted by javaeyes
    I now need to pass it again to another helper function. Do I need to pass it again by reference, or can I just pass it like a 'normal' variable, by value and derefence it.
    Here's the thing: what is "it"? Is "it" your struct object, or is "it" a pointer to your struct object?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    People will look at you funny if you talk about pass by reference in C; the orthodoxy is that everything is passed by value, because when you use a pointer, what you are doing is passing a memory address. Manipulating memory addresses directly is commonplace in C, so it makes sense to regard them as values, and not just references to the data they contain.

    References (which do not exist in C) and pointers are not quite the same thing, because while a pointer can serve as (the equivalent of) a reference (hence, it can be dereferenced), it has more uses beyond that. So you would say, "I have a pointer to a struct which I passed to a function. I now need to pass it again to another function." If you consider that for a moment, the answer to your question should be clear.*

    Do I need to pass it again by reference, or can I just pass it like a 'normal' variable, by value and derefence it. I think I can just pass it normally.
    You don't need to dereference pointers to structs, you can just use pointer notation with it:

    Code:
    struct whatever {
        int x;
    };
    struct whatever a, *p = &a;
    a.x = 666;  // normal struct notation uses .
    p->x *= 2; // pointer to struct notation uses ->
    * yes, you just pass the same address (pointer) on.
    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 javaeyes's Avatar
    Join Date
    Feb 2012
    Posts
    153
    I have struct instance in main named option. I pass it by reference like this: functionOne(&option). Within functionOne() I access it like tempoption->var1. I need to pass tempoption to functionTwo();
    should pass it like functionTwo(&tempoption) or functionTwo(tempoption).
    Hope that helps, to answer your question, "it" (I believe) is a pointer to the struct.
    P.s. I was confused by you last , very helpful, post: are you a man or a woman? Thanks Laser.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by javaeyes View Post
    I have struct instance in main named option. I pass it by reference like this: functionOne(&option).
    Nope, you are passing the address of (&), aka a pointer to, "option". See post #3.
    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

  6. #6
    Registered User javaeyes's Avatar
    Join Date
    Feb 2012
    Posts
    153
    MK, Thank you.
    I now know that I can just pass the pointer along. Which is pretty much what I though, although I wasn't using the correct parlance. Thanks for the vocab lesson, I really do want to sound like I know what I'm doing. Last question. I never actually create a pointer to the struct by name. I mean i don't do this *p = &mystruct and then pass it like myFunction(p) I'm just doing this: myFunction(&mystruct), Is that the same thing? Thanks.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by javaeyes View Post
    I'm just doing this: myFunction(&mystruct), Is that the same thing? Thanks.
    Yep. I just created a pointer to illustrate, that's not necessary for passing an address. A pointer is just a primitive type that holds a memory address (like an int holds a whole number), which is the value you are passing. Unlike references in other languages, there is nothing else under the hood. This is why a pointer is (generally) 4 bytes long on 32 bit systems and 8 bytes long on 64 bit systems.
    Last edited by MK27; 03-04-2012 at 10:19 AM.
    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

  8. #8
    Registered User javaeyes's Avatar
    Join Date
    Feb 2012
    Posts
    153
    I appreciate your time sir.
    MK27 likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem Modifying Struct Member Passed Into Function
    By soj0mq3 in forum C Programming
    Replies: 1
    Last Post: 04-27-2010, 01:07 AM
  2. passing struct to function
    By silentkarma in forum Windows Programming
    Replies: 15
    Last Post: 03-06-2008, 11:57 PM
  3. Acessing a struct after passing to a function
    By bomberto in forum C++ Programming
    Replies: 5
    Last Post: 10-04-2006, 04:29 PM
  4. passing struct to function help
    By staticalloc in forum C Programming
    Replies: 4
    Last Post: 10-06-2004, 08:30 AM
  5. passing struct to function
    By blight2c in forum C++ Programming
    Replies: 3
    Last Post: 03-15-2002, 11:52 PM

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