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

This is a discussion on Struct question... difference between passing to function... within the C++ Programming forums, part of the General Programming Boards category; Whats the difference between passing a struct by reference/not? struct editStruct (struct x) struct editStruct (struct &x) What are the ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    98

    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
    Posts
    5
    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
    Location
    Singapore
    Posts
    22,170
    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?
    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

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,765
    Declare a really big struct.
    Call a function lots of times.
    Compare.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    56
    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
    Posts
    98
    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
    Posts
    8,893
    Create a new thread for a new topic, please.
    All the buzzt!
    CornedBee

    "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 Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

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