what is the difference ? (pass by reference)

V

Vasileios

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
 
J

Jon Bell

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.
 
V

Vasileios

Nils Petter Vaskinn said:
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.
 
D

Dave O'Hearn

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?
 
F

friedrich

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
 
K

Kon Tantos

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 = ...)
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'.
 
V

Vasileios Zografos

Kon said:
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 = ...)


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'.

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

Thank you
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top