what is the difference ? (pass by reference)

Discussion in 'C++' started by Vasileios, Nov 27, 2003.

  1. Vasileios

    Vasileios Guest

    Hello,

    could someone tell me what is the difference between:


    1)

    int *data;

    void doSomething(*data)
    {

    }



    2)
    int data;
    void doSomething(&data)
    {

    }



    they both pass data by reference right?
    Or not?

    Thank you
    Vasileios
    Vasileios, Nov 27, 2003
    #1
    1. Advertising

  2. On Thu, 27 Nov 2003 04:52:34 -0800, Vasileios wrote:


    > void doSomething(*data)
    > void doSomething(&data)


    > they both pass data by reference right?


    No, one passes data by reference the other passes a reference to the data.

    --
    NPV

    "the large print giveth, and the small print taketh away"
    Tom Waits - Step right up
    Nils Petter Vaskinn, Nov 27, 2003
    #2
    1. Advertising

  3. Vasileios

    Grumble Guest

    Grumble, Nov 27, 2003
    #3
  4. Vasileios

    Jon Bell Guest

    In article <>,
    Vasileios <> wrote:
    >
    >1)
    >
    >int *data;
    >
    >void doSomething(*data)
    >{
    >
    >}
    >
    >
    >
    >2)
    >int data;
    >void doSomething(&data)
    >{
    >
    >}
    >
    >
    >
    >they both pass data by reference right?
    >Or not?


    Not. They're both invalid syntax.

    --
    Jon Bell <> Presbyterian College
    Dept. of Physics and Computer Science Clinton, South Carolina USA
    Jon Bell, Nov 27, 2003
    #4
  5. Vasileios

    Vasileios Guest

    "Nils Petter Vaskinn" <> wrote in message news:<>...
    > On Thu, 27 Nov 2003 04:52:34 -0800, Vasileios wrote:
    >
    >
    > > void doSomething(*data)
    > > void doSomething(&data)

    >
    > > they both pass data by reference right?

    >
    > No, one passes data by reference the other passes a reference to the data.



    Hmm... does this mean that with the first "void doSomething(*data)"
    any changes I make inside the function will change the data that is
    passed from outside, and the second "void doSomething(&data)" will
    only change the data inside the function?


    V.Z.
    Vasileios, Nov 28, 2003
    #5
  6. Vasileios

    Dave O'Hearn Guest

    (Vasileios) wrote:
    > Hmm... does this mean that with the first "void doSomething(*data)"
    > any changes I make inside the function will change the data that is
    > passed from outside, and the second "void doSomething(&data)" will
    > only change the data inside the function?


    It would help if you gave some info on how long you have been using
    C++. Are you familiar with pointers and trying to understand how
    references are different, or are you new to all of this stuff at once?

    --
    Dave O'Hearn
    Dave O'Hearn, Nov 29, 2003
    #6
  7. Vasileios

    friedrich Guest

    > 1)
    >
    > int *data;
    >
    > void doSomething(*data)


    This should be:

    void doSomething(int *data)


    > 2)
    > int data;
    > void doSomething(&data)


    This should be:

    void doSomething(int &data)

    In the first example you would have to use a pointer-to-int as the
    function argument, in the second, you could use an int as an argument
    and the function would automatically use a reference. If you used
    const:

    void doSomething(const int &data)

    that would protect the int from being changed outside of the function.

    Your function name looks like a Java style name. Are you a Java
    programmer?

    -FA
    friedrich, Nov 29, 2003
    #7
  8. Vasileios

    Kon Tantos Guest

    Vasileios wrote:
    > "Nils Petter Vaskinn" <> wrote in message news:<>...
    >
    >>On Thu, 27 Nov 2003 04:52:34 -0800, Vasileios wrote:
    >>
    >>
    >>
    >>>void doSomething(*data)

    this passes a copy of a reference (called a pointer). Your function can change 'data'
    without affecting the copy in the calling code. You can dereference 'data' to change
    data in the calling code (*data = ...)

    >>>void doSomething(&data)

    this passes a reference to data in the calling code. When you access data you are
    _actually_ working with the outside data. So data = ... directly changes whatever
    data is 'refering to'.

    >>
    >>
    >>
    >>>they both pass data by reference right?

    >>
    >>No, one passes data by reference the other passes a reference to the data.

    >
    >
    >
    > Hmm... does this mean that with the first "void doSomething(*data)"
    > any changes I make inside the function will change the data that is
    > passed from outside, and the second "void doSomething(&data)" will
    > only change the data inside the function?
    >
    >
    > V.Z.
    Kon Tantos, Nov 29, 2003
    #8
  9. Kon Tantos wrote:
    >
    >
    > Vasileios wrote:
    >
    >> "Nils Petter Vaskinn" <> wrote in message
    >> news:<>...
    >>
    >>> On Thu, 27 Nov 2003 04:52:34 -0800, Vasileios wrote:
    >>>
    >>>
    >>>
    >>>> void doSomething(*data)

    >
    > this passes a copy of a reference (called a pointer). Your function can
    > change 'data' without affecting the copy in the calling code. You can
    > dereference 'data' to change data in the calling code (*data = ...)
    >
    >>>> void doSomething(&data)

    >
    > this passes a reference to data in the calling code. When you access
    > data you are _actually_ working with the outside data. So data = ...
    > directly changes whatever data is 'refering to'.
    >
    >>>
    >>>
    >>>
    >>>> they both pass data by reference right?
    >>>
    >>>
    >>> No, one passes data by reference the other passes a reference to the
    >>> data.

    >>
    >>
    >>
    >>
    >> Hmm... does this mean that with the first "void doSomething(*data)"
    >> any changes I make inside the function will change the data that is
    >> passed from outside, and the second "void doSomething(&data)" will
    >> only change the data inside the function?
    >>
    >>
    >> V.Z.

    >
    >


    excellent. thats what I wanted to know.
    plain and simple.

    Thank you
    Vasileios Zografos, Nov 29, 2003
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jerry
    Replies:
    20
    Views:
    7,930
    Roedy Green
    Sep 9, 2005
  2. blufox
    Replies:
    2
    Views:
    552
  3. Mr A
    Replies:
    111
    Views:
    2,108
  4. Robert
    Replies:
    10
    Views:
    1,367
    E. Robert Tisdale
    Aug 24, 2005
  5. LuB
    Replies:
    6
    Views:
    452
    Mike Wahler
    Sep 23, 2005
Loading...

Share This Page