473,779 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7188
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********@gma il.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.goo glegroups.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 "declarativ e 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.goo glegroups.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 "declarativ e 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

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

Similar topics

2
16245
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 integer? Or Is it a reference to a pointer to a constant integer? What is being constant in this case? The pointer or the integer being pointed? How about int * const & ? Is this a reference to a constant pointer to an integer? Or
39
2423
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 the rest of it means...". Still, I have some questions, that are frustrating me:- Grateful for any comments. 1. What is the difference between #include <iostream> // (or any include file) which is used in this
1
1423
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 and ending with BOOK ENDING HERE. All that text between is just a copy from the book. My question come after the text section. The book says
4
3409
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 42. They say that 42 is printed the second time since the value was wrapped in a class and therefore became passed by reference. (sorry for any typos I am a newbie here ;-)
16
2506
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 SRB_HaId;
4
343
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 loaded. Will that object's value be accessible during consequent postbacks or is it set to null after HTML is rendered initially?
10
16675
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
4044
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 tells me if a variable has changed, give me the original and current value, and whether the current value and original value is/was null or not. This one works fine but is recreating the same methods over and over for each variable type. ...
30
1911
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 some light on this! (code abbreviated for clarity) I have a parent class, DetailCollection, with a child class KeycodeTracker:
0
9632
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9471
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10302
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8958
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7478
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6723
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4036
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2867
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.