473,386 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

what is the difference ? (pass by reference)

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
8 1247
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
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
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
"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
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
> 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


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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
8
by: Chris Mayers | last post by:
'out' and 'ref' parameters in C#... Both these can be used to pass parameter values BACK from a Method, but obviously they are different techniques. As I understand it, 'ref' parameter...
14
by: bo | last post by:
And why and where one should use one vs. the other? Verbally, it seems like semantics to me--but obviously there is some actual difference that makes references different and or preferable over...
11
by: modemer | last post by:
If I define the following codes: void f(const MyClass & in) {cout << "f(const)\n";} void f(MyClass in) {cout<<"f()\n";} MyClass myclass; f(myclass); Compiler complain that it can't find...
5
by: Chris | last post by:
Hi, I don't get the difference between a struct and a class ! ok, I know that a struct is a value type, the other a reference type, I understand the technical differences between both, but...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
7
by: amit | last post by:
Hello everybody, I need your advice on this. In my javascript I'm using two anchor <A> which both are to download a pdf file. That works fine but my question is why the "this" parameter in...
10
by: ravi | last post by:
Hi, i am a c++ programmer, now i want to learn programming in c also. so can anybody explain me the difference b/w call by reference and call by pointer (with example if possible).
11
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.