Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > what is the difference ? (pass by reference)

Reply
Thread Tools

what is the difference ? (pass by reference)

 
 
Vasileios
Guest
Posts: n/a
 
      11-27-2003
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
 
Reply With Quote
 
 
 
 
Nils Petter Vaskinn
Guest
Posts: n/a
 
      11-27-2003
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

 
Reply With Quote
 
 
 
 
Grumble
Guest
Posts: n/a
 
      11-27-2003
Vasileios wrote:
> could someone tell me what is the difference between:


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

 
Reply With Quote
 
Jon Bell
Guest
Posts: n/a
 
      11-27-2003
In article <(E-Mail Removed) >,
Vasileios <(E-Mail Removed)> 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 <(E-Mail Removed)> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
 
Reply With Quote
 
Vasileios
Guest
Posts: n/a
 
      11-28-2003
"Nils Petter Vaskinn" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed) 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.
 
Reply With Quote
 
Dave O'Hearn
Guest
Posts: n/a
 
      11-29-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (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
 
Reply With Quote
 
friedrich
Guest
Posts: n/a
 
      11-29-2003
> 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
 
Reply With Quote
 
Kon Tantos
Guest
Posts: n/a
 
      11-29-2003


Vasileios wrote:
> "Nils Petter Vaskinn" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed) 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.


 
Reply With Quote
 
Vasileios Zografos
Guest
Posts: n/a
 
      11-29-2003
Kon Tantos wrote:
>
>
> Vasileios wrote:
>
>> "Nils Petter Vaskinn" <(E-Mail Removed)> wrote in message
>> news:<(E-Mail Removed) 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
difference between Wifi repeater and range extender? AdminKen Wireless Networking 4 02-11-2011 04:05 AM
wireless speed difference =?Utf-8?B?dG9t?= Wireless Networking 6 06-02-2005 02:16 AM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM
difference between 802.3b and 802.11b Tony Warburton Wireless Networking 2 11-25-2004 10:27 PM
Difference : WEP open / shared ? Michael van den Berg Wireless Networking 1 08-18-2004 07:36 PM



Advertisments