473,385 Members | 1,615 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,385 software developers and data experts.

Pass by Pointer * or Pass by Reference &?

Hello all,

I am a C programmer learning C++. And I am confused with function Pass
by Pointer * or Pass by Reference &.

Function pass by pointer like:
void func(int * x)
And function pass by referencelike:
void func(int & x)

What's their main difference and be used in what circumstance?

Best regards,
Robert

Aug 19 '05 #1
10 40625
"Robert" <zh*******@gmail.com> schrieb im Newsbeitrag
news:11*********************@g14g2000cwa.googlegro ups.com...
Hello all,

I am a C programmer learning C++. And I am confused with function Pass
by Pointer * or Pass by Reference &.

Function pass by pointer like:
void func(int * x)
And function pass by referencelike:
void func(int & x)

What's their main difference and be used in what circumstance?

Best regards,
Robert

Pointer can be null, references can't. Pointers can be reassigned.
A reference is more comfortable to use. You can handle it like a normal
object. When changing a function from
void func(const AClass);
to
void func(const AClass&);
you do not need to rewrite your code.

Greetings Chris
Aug 19 '05 #2
Robert wrote:
Hello all,

I am a C programmer learning C++. And I am confused with function Pass
by Pointer * or Pass by Reference &.

Function pass by pointer like:
void func(int * x)
And function pass by referencelike:
void func(int & x)

What's their main difference and be used in what circumstance?

Best regards,
Robert


There is a lot of good information about this in this handy guide:

http://www.parashift.com/c++-faq-lite/

--John Ratliff
Aug 19 '05 #3
Another problem is why reference cannot be able to represent a NULL
object?

Best regards,
Robert

Aug 19 '05 #4
One other difference between pointers and references is that if you use
dynamic_cast on an incorrect reference it willl throw a bad cast
exception. On the other hand, an incorrect dynamic cast on a pointer
will not throw a bad cast exception.

void function(Panda& q)
{
Panda &ip = dynamic_cast<randomref&)(q);
}

void g()
{
try{
f(*(new Panda))
}
catch(bad_cast)
{

}
}

Aug 20 '05 #5
Robert ha scritto:
Another problem is why reference cannot be able to represent a NULL
object?

Best regards,
Robert


References are intended to "point" to existing objects :

the function

void f(T &t)
{
[...]
}

is similar to

void f(T const *t)
{
assert(t != NULL);
[...]
}

Reference can't be null and its address can't be incremented.

Bye,
Giulio
Aug 22 '05 #6
Frank Chang schreef:
One other difference between pointers and references is that if you use
dynamic_cast on an incorrect reference it willl throw a bad cast
exception. On the other hand, an incorrect dynamic cast on a pointer
will not throw a bad cast exception.


No, it returns a NULL pointer. This can be useful:

void foo( Base* b )
{
if( Derived* d = dynamic_cast<Derived>(b) )
{
// do something with Derived
d->bar();
}
else
{
// User didn't have a Derived object, but we don't care why not
}
}
This is a quite common idiom. I usually leave out the comment and the
else block if there is no default action. The idea is that I don't
want to differentiate between a non-Derived Base object or no object
at all. I purely want to know if the caller has a Derived object.

HTH,
Michiel Salters

Aug 22 '05 #7
no it doesn't. On a pointer it returns null on references it throws
std::bad_cast

Aug 22 '05 #8

"Robert" <zh*******@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
Another problem is why reference cannot be able to represent a NULL
object?

Best regards,
Robert


A reference can't refer to a null object since its permanently bound to a
valid object. This is an important distinction (unlike a pointer which can
point to another object or even to a null object). A reference is a
permanent alias to a valid object.

In fact, the lifetime of any object may even be extended to satisfy this
object-is-valid rule.

Aug 24 '05 #9

"Robert" <zh*******@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
Another problem is why reference cannot be able to represent a NULL
object?

Best regards,
Robert


Define what a NULL object is.

Dan
Aug 24 '05 #10
Robert wrote:
Another problem is
why reference cannot be able to represent [an invalid] object? cat main.cc #include <iostream>

void f(const int& r) {
if (0 == &r) { // undefined behavior
std::cout << "&r = " << &r << std::endl;
}
else {
std::cout << "r = " << r << std::endl;
}
}

int main(int argc, char* argv[]) {

int* p = 0;
int& r = *p; // undefined behavior
f(r);

return 0;
}
g++ -Wall -ansi -pedantic -o main main.cc
./main

&r = 0

Beware!
A reference *can* represent an invalid object
but the behavior is undefined.
Aug 24 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: sam pal | last post by:
I have the following programs and please clear my doubts. Passing the value by reference is same as pointer by reference. Is this true? What is the difference between display and display2? How...
2
by: Nelson | last post by:
Hi, Can pointers be passed by reference to functions? If so, how to do it? Or is there any other way to change the pointers in the caller by the called function? Thanks, Nelson
3
by: dover | last post by:
There are two situations under which I am not sure which one to use, pointer or reference: Passing parameter into a function A object is aggregated in another one. Is there any general rules...
41
by: Berk Birand | last post by:
Hi, I am just learning about the array/pointer duality in C/C++. I couldn't help wondering, is there a way to pass an array by value? It seems like the only way to do is to pass it by...
5
by: DamonChong | last post by:
Hi, I am still struggling to master C++ and is trying to understand how to achieve passing arguments using pointers. I got some questions that I like to post to the experts here, hope you can...
1
by: Scott McFadden | last post by:
What is the proper way to pass pointers by reference from managed c++ calls to native c++ calls? In managed C++, I have a pointer to an array of structures, that I pass to a native c++ method by...
3
by: Scott | last post by:
Hello, I am having some difficulty with the following C++: I have a declared a pointer to Foo like so: Foo *pFoo; Now, I have an API object that I try to use like so:
29
by: shuisheng | last post by:
Dear All, The problem of choosing pointer or reference is always confusing me. Would you please give me some suggestion on it. I appreciate your kind help. For example, I'd like to convert a...
18
by: happyvalley | last post by:
Hi, basically, the test function get a char pointer, and assigned a string to it. then the string is passed back by the call-by-reference mechanism. in test(), I reallocate some memory for the...
11
by: HSeganfredo | last post by:
Folks, I want to write a init string function that mallocs an area, fills it with a char, sticks a NUL char in the last position and returns it to the user. So far I noticed that my...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...

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.