Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > References and pointers

Reply
Thread Tools

References and pointers

 
 
Simon Saize
Guest
Posts: n/a
 
      12-31-2007
Hi -

What's the point of having references (&)? Why don't we just use
pointers (*)?

Thanks.
 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      12-31-2007
Simon Saize said:

> Hi -
>
> What's the point of having references (&)?


In C, they don't exist. I suggest you remove comp.lang.c from the
crosspost.

> Why don't we just use pointers (*)?


We do.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
 
 
 
Daniel T.
Guest
Posts: n/a
 
      12-31-2007
Simon Saize <(E-Mail Removed)> wrote:

> What's the point of having references (&)? Why don't we just use
> pointers (*)?


References were added to the language to facilitate operator overloading
in C++. Do they even exist in C?
 
Reply With Quote
 
Simon Saize
Guest
Posts: n/a
 
      12-31-2007
I believe they exist in some versions of C, e.g. lcc-win which also
supports Operator Overloading.

On 31/12/2007 18:43, Daniel T. wrote:
> Simon Saize <(E-Mail Removed)> wrote:
>
>
>>What's the point of having references (&)? Why don't we just use
>>pointers (*)?

>
>
> References were added to the language to facilitate operator overloading
> in C++. Do they even exist in C?

 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      12-31-2007
Simon Saize wrote:
> Hi -
>
> What's the point of having references (&)? Why don't we just use
> pointers (*)?


There really was no need to post this to comp.lang.c. In C we have no
"references", so the question is pointless. If your aim is to actually
learn about the rationale for C++ using these (and there is one,
although it is because of other features of C++ that we in comp.lang.c
do without quite happily), the folks in comp.lang.c++ should be able to
help. However, your crossposting suggests that your aim is to start
another pointless language war; if so, I hope no one rises to the bait.

Follow-ups restricted to comp.lang.c++, where the question actually
makes sense.
 
Reply With Quote
 
Alexey Stepanyan
Guest
Posts: n/a
 
      12-31-2007
On 31 , 21:31, Simon Saize <(E-Mail Removed)> wrote:
> Hi -
>
> What's the point of having references (&)? Why don't we just use
> pointers (*)?
>
> Thanks.


The sense of using references in ++ instead of pointers is to avoid
annoying checking pointers for NULL

For example if you have a function that accepts reference as a
paramater you do not have to check the reference
for NULL ( 0 ) before using it because references cannot be NULL but
if you have a pointer you should always check it
for NULL before you invoke member functions otherwise you might get
access violation error.
 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      12-31-2007
Simon Saize wrote:
> I believe they exist in some versions of C, e.g. lcc-win which also
> supports Operator Overloading.
>


Yes, lcc-win supports references and some other good ideas from C++.

--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      12-31-2007
> >
> > What's the point of having references (&)? Why don't we just use
> > pointers (*)?
> >


> The sense of using references in ++ instead of pointers is to avoid
> annoying checking pointers for NULL


> For example if you have a function that accepts reference as a
> paramater you do not have to check the reference
> for NULL ( 0 ) before using it because references cannot be NULL but
> if you have a pointer you should always check it
> for NULL before you invoke member functions otherwise you might get
> access violation error.


I have to disagree about the need to always check pointers for NULL. A
program should only need to check for NULL pointers if the design of the
program intentionally allows and expects pointers to be NULL under certain
conditions. If there is a requirement (by the calling code) that the
pointer it wants to use is not NULL, then any NULL pointer you encounter is
an ERROR, and it's just as acceptable to let it crash by using it as it is
to check it and then throw (or whatever). Unexpected NULL pointers are best
handled (in my opinion) by assertions, to help (but not guarantee) catching
programming mistakes during development. If you have production code which
generates a NULL pointer when a NULL pointer is not expected and allowed,
then you need to fix the part of the program that allowed the NULL to occur
in the first place, not add code to check if it's NULL. Of course, if your
program expects and allows NULL pointers on purpose, then checking for that
condition is perfectly acceptable. But in general, adding NULL pointer
checks everywhere is a poor practice, IMO.

-Howard


 
Reply With Quote
 
cr88192
Guest
Posts: n/a
 
      12-31-2007
"Alexey Stepanyan" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
On 31 ???, 21:31, Simon Saize <(E-Mail Removed)> wrote:
> Hi -
>
> What's the point of having references (&)? Why don't we just use
> pointers (*)?
>
> Thanks.


<
The sense of using references in ?++ instead of pointers is to avoid
annoying checking pointers for NULL

For example if you have a function that accepts reference as a
paramater you do not have to check the reference
for NULL ( 0 ) before using it because references cannot be NULL but
if you have a pointer you should always check it
for NULL before you invoke member functions otherwise you might get
access violation error.
>


as noted by others, references are non-standard in C (as such, they are only
really valid in C++, and it is good not to confuse C and C++, as they are
different languages).


however, it is also nicer to be able to type:
x=3;
than:
*rx=3;

this can actually matter some with more non-trivial argument types (char as
'char *'), where the common practice becomes to extract the value from the
pointer, work on it, and store it back before return (this is a very common
pattern in things like parsers, ...).

reason:
int foo(char **rs)
{
....
}

rs++; //wont do what you want
*rs++; //seems like it would work, but it does not (does something
different)
(*rs)++; //is problematic IME.

so, afaik, about the only really safe option here is:
*rs=*rs+1;

so, common is to have to be like:
int foo(char **rs)
{
char *s;
....
s=*rs;
....
*rs=s;
return(i);
}


so, references can have some uses...



 
Reply With Quote
 
Salt_Peter
Guest
Posts: n/a
 
      12-31-2007
On Dec 31, 1:31 pm, Simon Saize <(E-Mail Removed)> wrote:
> Hi -
>
> What's the point of having references (&)? Why don't we just use
> pointers (*)?
>
> Thanks.


Because references can do things pointers can't in C++ (the reverse is
true as well).
C++ references benefits includes declaring and implementing operators.
They also support polymorphic calls, just like pointers do.
References make the code's intentions clear and unambiguous.
(doesn't mean C++ is better, in some respects it does mean its safer)
References can offer guarentees that pointers can't.
For example:

void foo(const int& r)
{
// do stuff
}

guarentees that reference r, if accessed within that function body,
will refer to the original integer.
That relationship is indestructeable.

On the other hand, since a pointer_to_const is reseatable:

void foo(const int* p)
{
int x(8;
p = &x; // oops
// do stuff
}

and a const pointer_to_const can be const_cast away:

void foo(const int* const p)
{
int x(8;
int* p_ = const_cast<int*>(p);
p_ = &x; // oops
// do stuff
}

a pointer of any flavour is unable to offer the same guarentee a
reference can.
void foo(const int& r) clearly states the intentions of the code.
There is no uncertainty.
 
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
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
Typedef A references struct B which references struct A which... DanielEKFA C++ 8 05-16-2005 10:26 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
how to understand references to variables and references to constants are distinguished? baumann.Pan@gmail.com C++ 3 11-10-2004 04:16 AM
Pointers and References (and References to Pointers) Roger Leigh C++ 8 11-17-2003 10:14 AM



Advertisments