By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,402 Members | 1,041 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,402 IT Pros & Developers. It's quick & easy.

what is the difference ? (pass by reference)

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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

Jul 22 '05 #2

P: n/a
Vasileios wrote:
could someone tell me what is the difference between:


Does this help?
http://www.parashift.com/c++-faq-lite/references.html

Jul 22 '05 #3

P: n/a
In article <40**************************@posting.google.com >,
Vasileios <va*******@zografos.org> 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 <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 22 '05 #4

P: n/a
"Nils Petter Vaskinn" <no@spam.for.me.invalid> wrote in message news:<pa****************************@spam.for.me.i nvalid>...
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.
Jul 22 '05 #5

P: n/a
va*******@zografos.org (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
Jul 22 '05 #6

P: n/a
> 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
Jul 22 '05 #7

P: n/a


Vasileios wrote:
"Nils Petter Vaskinn" <no@spam.for.me.invalid> wrote in message news:<pa****************************@spam.for.me.i nvalid>...
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.


Jul 22 '05 #8

P: n/a
Kon Tantos wrote:


Vasileios wrote:
"Nils Petter Vaskinn" <no@spam.for.me.invalid> wrote in message
news:<pa****************************@spam.for.me.i nvalid>...
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

Jul 22 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.