Thread: Struct question... difference between passing to function...

  1. #1
    Registered User
    Join Date
    Oct 2008

    Struct question... difference between passing to function...

    Whats the difference between passing a struct by reference/not?

    struct editStruct (struct x)
    struct editStruct (struct &x)

    What are the differences in passing these two?

  2. #2
    Registered User
    Join Date
    Feb 2009
    as far as I know, passing something by reference passes a pointer so that any changes made in the function are actually made to the variable, whereas without the & means that any changes are lost unless the variable is returned at the end and saved.

    I assume that it is the same with structs...

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Weirdly, finding a tutorial to explain references seems hard... the word "reference" is just too overloaded.

    Anyway, what do you know of references? What about say, passing an int by reference?
    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

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Declare a really big struct.
    Call a function lots of times.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Oct 2007
    wont one edit the value(&) and permanenlty change it wherease the other jsut copies and uses the value. im thinking the same as passing to functions here

  6. #6
    Registered User
    Join Date
    Oct 2008
    Ok the people saying passing by reference lets you change the actual value, whereas not makes a copy are correct I believe.

    [edit3] - Found the answer to my second Q, thanks though CornedBee.
    Last edited by Sparrowhawk; 02-23-2009 at 04:03 PM.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Create a new thread for a new topic, please.
    All the buzzt!

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM