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

  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
    28,413
    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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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 11: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.

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-07-2008, 12:57 AM
  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-16-2002, 12:52 AM