Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > 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
 
 
 
 
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
 
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
 
CBFalconer
Guest
Posts: n/a
 
      01-01-2008
jacob navia wrote:
> 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++.


However lcc-win is not a C compiler. No compiler that supports
references is a C compiler.

Follow-up set to c.l.c. c.l.c++ crossposts are idiotic.

--
Merry Christmas, Happy Hanukah, Happy New Year
Joyeux Noel, Bonne Annee, Frohe Weihnachten
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      01-01-2008
cr88192 wrote:
> Simon Saize <(E-Mail Removed)> wrote:
>
>> 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


There are NO references in the C language. None.

Follow-ups set to remove the idiotic C and C++ cross-post.

--
Merry Christmas, Happy Hanukah, Happy New Year
Joyeux Noel, Bonne Annee, Frohe Weihnachten
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>


--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      01-01-2008
CBFalconer said:

> jacob navia wrote:
>> 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++.

>
> However lcc-win is not a C compiler. No compiler that supports
> references is a C compiler.


Wrong. The Standard does not forbid compilers from offering extensions, as
long as (a) required diagnostic messages are still produced and (b)
strictly conforming programs are not affected. If lcc-win32, when invoked
in conforming mode, diagnoses C++ reference syntax as being erroneous,
then that's all it has to do to remain conforming.

--
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
 
 
 
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