473,386 Members | 1,803 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.

old question: difference between pointer and reference

If the people ask what is the different between pointer and reference,
what is the brief and good answer?
I say " pointer could point to NULL, but there is no null reference",
What is your opinion?

Vol

Aug 1 '06 #1
14 7153
Vols wrote:
If the people ask what is the different between pointer and reference,
what is the brief and good answer?
"Yes".
I say " pointer could point to NULL, but there is no null reference",
What is your opinion?
See above.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 1 '06 #2
Your answer doesn't make sense.

Vol.
Victor Bazarov wrote:
Vols wrote:
If the people ask what is the different between pointer and reference,
what is the brief and good answer?

"Yes".
I say " pointer could point to NULL, but there is no null reference",
What is your opinion?

See above.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 1 '06 #3
Vols wrote:
Your answer doesn't make sense.
Don't top post.

And if *you* can't make sense of it, it doens't mean there isn't any.

If you need to form your own opinion about the differences, read the
FAQ, read the archives, search the web for answers. We don't need to
beat this dead horse again.
>
Vol.
Victor Bazarov wrote:
>Vols wrote:
>>If the people ask what is the different between pointer and
reference, what is the brief and good answer?

"Yes".
>>I say " pointer could point to NULL, but there is no null
reference", What is your opinion?

See above.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 1 '06 #4
according to the following
http://en.wikipedia.org/wiki/Reference_%28C%2B%2B%29
one reads :

C++ references differ from pointers in several essential ways:

* It is not possible to refer to a reference object directly after
it is defined; any occurrence of its name refers directly to the
object it references.
* As a consequence of the first point, neither arithmetic, casts,
nor any other operation can be performed on references except copying
their binding into other references.
* Once a reference is created, it cannot be later made to
reference another object; we say it cannot be reseated. This is often
done with pointers.
* References cannot be null, whereas pointers can; every reference
refers to some object, although it may or may not be valid.
* References cannot be uninitialized. Because it is impossible to
reinitialize a reference, they must be initialized as soon as they are
created. In particular, local and global variables must be initialized
where they are defined, and references which are data members of class
instances must be initialized in the class's constructor.

There is a simple conversion between pointers and references: the
address-of operator (&) will yield a pointer referring to the same
object when applied to a reference, and a reference which is
initialized from the dereference (*) of a pointer value will refer to
the same object as that pointer, where this is possible without
invoking undefined behavior. This equivalence is a reflection of the
typical implementation, which effectively compiles references into
pointers which are implicitly dereferenced at each use.

A consequence of this is that in many implementations, operating on a
variable with automatic or static lifetime through a reference,
although syntactically similar to accessing it directly, can involve
hidden dereference operations that are costly.

Also, because the operations on references are so limited, they are
also much easier to reason about formally than pointers, and harder to
cause errors with. While pointers can be made invalid through a
variety of mechanisms, ranging from carrying a null value to
out-of-bounds arithmetic to illegal casts to producing them from
random integers, a reference only becomes invalid in two cases:

* If it refers to an object with automatic allocation which goes
out of scope,
* If it refers to an object inside a block of dynamic memory which
has been freed.

The first is easy to detect automatically due to static scoping of
variables; the second is more difficult to assure, but it is the only
concern with references, and one suitably addressed by a reasonable
allocation policy.
hope it helps
friendly
jerome

On 1 Aug 2006 08:46:23 -0700, "Vols" <vo********@gmail.comwrote:
>If the people ask what is the different between pointer and reference,
what is the brief and good answer?
I say " pointer could point to NULL, but there is no null reference",
What is your opinion?

Vol
Aug 1 '06 #5
Vols wrote:
If the people ask what is the different between pointer and reference,
what is the brief and good answer?
I say " pointer could point to NULL, but there is no null reference",
What is your opinion?

Vol
References are syntactical sugar to make the programmers life easier.
In the case of a const-reference as a parameter to a function you can even
pass non-lvalues. With pointers this isn't possible.

Aug 1 '06 #6
Vols posted:
If the people ask what is the different between pointer and reference,
what is the brief and good answer?
I say " pointer could point to NULL, but there is no null reference",
What is your opinion?

Let's start off with a guinea pig object:

int obj;

I suppose you could say that the following two definitions are equivalent:

(1) int &r = obj;

(2) int *const p = obj;

Except that:

(A) The first one can't be null.

(B) You must dereference the second one to get at the object, e.g.:

int i;

i = r;
i = *p;

A "reference to const" has further functionality in that it can bind to an
R-value:

int const &r = 5; /* OK */

int const *const p = &5;
/* ERROR: Can't take address of an R-value */

You might think that it doesn't make sense to be able to have a reference
to an R-value, but C++ imposes special treatment for "references to const".
It treats the following:

int const &r = 5;

As if you had written:

int const val = 5;
int const &r = val;

Similarly, if you want to bind a reference to the return value of a
function:

SomeType SomeFunc();

int main()
{
SomeType const &r = SomeFunc();
}
Then it's as if you had written:

SomeType SomeFunc();

int main()
{
SomeType const obj( SomeFunc() );

SomeType const &r = obj;
}

--

Frederick Gotham
Aug 1 '06 #7
Vols wrote:
If the people ask what is the different between pointer and reference,
what is the brief and good answer?
Brief and good answer:

In the context of C++, a reference is a declarative and type casting
gadget which causes an object itself to be propagated rather than a
copy of its value. A pointer is a first-class data type for
representing the storage location of an object.

Aug 1 '06 #8

"Kaz Kylheku" <kk******@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
Vols wrote:
>If the people ask what is the different between pointer and reference,
what is the brief and good answer?

Brief and good answer:

In the context of C++, a reference is a declarative and type casting
gadget which causes an object itself to be propagated rather than a
copy of its value.
What??? What's a "declarative and type casting gadget", and what does a
reference have to do with type casting?

A reference is simply an "alias" for an existing object.

-Howard
Aug 1 '06 #9
Howard wrote:
"Kaz Kylheku" <kk******@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
>Vols wrote:
>>If the people ask what is the different between pointer and
reference, what is the brief and good answer?

Brief and good answer:

In the context of C++, a reference is a declarative and type casting
gadget which causes an object itself to be propagated rather than a
copy of its value.

What??? What's a "declarative and type casting gadget", and what
does a reference have to do with type casting?

A reference is simply an "alias" for an existing object.
I think you need to remember that references help using objects
polymorphically.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 1 '06 #10

"Victor Bazarov" <v.********@comAcast.netwrote in message
news:ea**********@news.datemas.de...
Howard wrote:
>"Kaz Kylheku" <kk******@gmail.comwrote in message
news:11**********************@m73g2000cwd.googleg roups.com...
>>Vols wrote:
If the people ask what is the different between pointer and
reference, what is the brief and good answer?

Brief and good answer:

In the context of C++, a reference is a declarative and type casting
gadget which causes an object itself to be propagated rather than a
copy of its value.

What??? What's a "declarative and type casting gadget", and what
does a reference have to do with type casting?

A reference is simply an "alias" for an existing object.

I think you need to remember that references help using objects
polymorphically.
True. I probably should have left out "simply" in my answer. A good C++
book is undoubtedly the best place for a complete description of references,
(not someone's "brief and good answer").

-Howard


Aug 1 '06 #11
Howard wrote:
True. I probably should have left out "simply" in my answer.
Or you should have simply left out your clueless answer.

Aug 1 '06 #12

"Kaz Kylheku" <kk******@gmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
Howard wrote:
>True. I probably should have left out "simply" in my answer.

Or you should have simply left out your clueless answer.
Instead of a personal attack, you could let us know what you meant by your
own answer. Specifically, what do you mean by: "a declarative and type
casting gadget".

Also, it's not that clear what you meant when you said it "causes an object
itself to be propogated". A reference "refers" to another object. It's an
alias for that object. Propogation implies some sort of movement or
copying.

-Howard

Aug 1 '06 #13

Howard wrote:
"Kaz Kylheku" <kk******@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
Vols wrote:
If the people ask what is the different between pointer and reference,
what is the brief and good answer?
Brief and good answer:

In the context of C++, a reference is a declarative and type casting
gadget which causes an object itself to be propagated rather than a
copy of its value.

What??? What's a "declarative and type casting gadget", and what does a
English lesson:

A gadget is a mechanism; a device. In a programming language, that is
syntax plus semantics. Declarative means having to do with
declarations, and type casting means having to do with type casts.
reference have to do with type casting?
Gee, I don't know! How about:

void func(Base &b)
{
Derived d = dynamic_cast<Derived &>(b);
}

That is a reference cast. Here, the reference isn't used to declare
anything. So if I had said that references were declarative gadget, I
would have missed the type casting use.
A reference is simply an "alias" for an existing object.
You're just parroting an oft repeated statement that only applies to
block scope reference variables. It is misleading to varying degrees if
applied to other situations. Entities which are not object-like can be
attributed as entities, such as function return values.

Also, when would an alias ever be constructed for a NON-existing
object?

Aug 1 '06 #14

"Kaz Kylheku" <kk******@gmail.comwrote in message
news:11**********************@m79g2000cwm.googlegr oups.com...
>
Howard wrote:
>"Kaz Kylheku" <kk******@gmail.comwrote in message
news:11**********************@m73g2000cwd.googleg roups.com...
Vols wrote:
If the people ask what is the different between pointer and reference,
what is the brief and good answer?

Brief and good answer:

In the context of C++, a reference is a declarative and type casting
gadget which causes an object itself to be propagated rather than a
copy of its value.

What??? What's a "declarative and type casting gadget", and what does a

English lesson:

A gadget is a mechanism; a device. In a programming language, that is
syntax plus semantics. Declarative means having to do with
declarations, and type casting means having to do with type casts.
>reference have to do with type casting?

Gee, I don't know! How about:

void func(Base &b)
{
Derived d = dynamic_cast<Derived &>(b);
}

That is a reference cast. Here, the reference isn't used to declare
anything. So if I had said that references were declarative gadget, I
would have missed the type casting use.
The fact that you can cast something to a reference doesn't make the
reference a "type casting gadget". Your original statement was made to
someone who is a newbie, who didn't understand what a reference was. I
think your explanation was not very helpful in that context. That's why I
asked you to clarify it. I apologize if my reply sounded arrogant (esp. by
using the ???).
>A reference is simply an "alias" for an existing object.

You're just parroting an oft repeated statement that only applies to
block scope reference variables. It is misleading to varying degrees if
applied to other situations. Entities which are not object-like can be
attributed as entities, such as function return values.

Also, when would an alias ever be constructed for a NON-existing
object?
Oh, come on!
I don't mind discussing issues here, even if I'm shown to be wrong. But can
we keep this on a professional level?

-Howard


Aug 1 '06 #15

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

Similar topics

2
by: CoolPint | last post by:
Can anyone clearly explain the difference between constant reference to pointers and reference to constant pointers? What is const int * & ? Is it a constant reference to a pointer to an...
39
by: TonyJeffs | last post by:
Great book - I like the way that unlike other books, AC++ explains as much as possible about every piece of code discussed, so I'm not left thinking, "well...OK... I get line 12, but I wonder what...
1
by: Tony Johansson | last post by:
Hello! I'm reading a book about C++ and there is something that I don't understand so I ask you. I have marked the section from the book that is of intertest by tagging it with BOOK START HERE...
4
by: z_learning_tester | last post by:
I'm reading the MS press C# book and there seems to be a contradiction. Please tell me which one is correct, 1 or 2. Thanks! Jeff 1. First it gives the code below saying that it prints 0 then...
16
by: Duncan Mole | last post by:
Hi, This is probably an easy one but it iy first bit of p/invoke. I am trying to use the following C struct in a call: typedef struct { BYTE SRB_Cmd; BYTE SRB_Status, BYTE ...
4
by: Diffident | last post by:
Hello All, I have a question which is pertinent to Page's lifecycle. I declared a protected static object (global variable within that class) whose value is set only once when the page is...
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).
20
by: tshad | last post by:
Using VS 2003, I am trying to take a class that I created to create new variable types to handle nulls and track changes to standard variable types. This is for use with database variables. This...
30
by: Logos | last post by:
I have what may be a bug, or may be a misunderstanding on how pass by reference and class inheritance works in PHP. Since I'm relatively new to PHP, I'm hoping for a little outside help to shed...
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: 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: 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
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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.