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

& vs ptr

P: n/a
what is the difference between passing by & and passing by pointer
for example
doSomething(int & address);
doSomething(int * ptr);
thanks
Jul 22 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Steve wrote:
what is the difference between passing by & and passing by pointer
for example
doSomething(int & address);
Why "address"? A reference is not an address, it's an alias.
doSomething(int * ptr);


Basically the same difference as in declaring a reference versus
declaring a pointer

int main() {
double d;
double& rd = d;
double* pd = &d;
}

Performance-wise, they are often the same. Logically, they are,
because pointers can be null, references can't, so when I receive
an argument by address (a pointer by value), I need to test it for
being non-null before using. I don't have to do that with references.

Of course, the syntax is different for accessing the object. If you
have a reference, you just use its name to get to the object. If you
have a pointer you need to dereference it using the * operator. Also,
a reference like you described can only refer to a single object, but
a pointer could be the pointer to the first element of an array of
several objects and you may get to other elements of that array by
means of the indexing operator ([])...

Victor
Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.