470,620 Members | 1,645 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,620 developers. It's quick & easy.

pointer variable vs reference variables

dear all,
can anyone explain the differences between pointer variable and
reference variables ?
Jun 27 '08 #1
9 2147
su***********@gmail.com wrote:
dear all,
can anyone explain the differences between pointer variable and
reference variables ?
You'll have to ask in a C++ group (comp.lang.c++) or in a group
appropriate to whatever language you are using (presumably it contains
both pointers and references, hence your question). C has no
references. If you want language independent responses, then
comp.programming might be a better place to post this question.

Jun 27 '08 #2
On Jun 7, 8:25*am, sulekhaswe...@gmail.com wrote:
dear all,

can anyone explain the differences between pointer variable and
reference variables ?

References don't exist in C, they're a C++ thing. A reference is
simply a pointer with the following features:
* You don't have to dereference it, it gets dereferenced
automagically
* You can't change what it points to

So the following two are equivalent:

void Func(int const *p) { *p = 5; }

void Func(int &i) { p = 5; }

If you want references in C, then you can play around with macroes:

void Func(int const *p)
{
# define i (*p)

i = 5;
}
Jun 27 '08 #3
* * void Func(int &i) { p = 5; }

i = 5;
Jun 27 '08 #4
On Jun 7, 9:05*am, Tomás Ó hÉilidhe <t...@lavabit.comwrote:
* * void Func(int const *p) { *p = 5; }

int *const p

* * void Func(int const *p)

int *const p

I should go back to bed.
Jun 27 '08 #5

<su***********@gmail.comwrote in message news:
dear all,
can anyone explain the differences between pointer variable and
reference variables ?
References exist in Java and C++, and are basically syntactic sugar for
pointers. They point only to one object, and may never be null. However the
variable is passed as an address, ie a pointer.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Jun 27 '08 #6
su***********@gmail.com ha scritto:
can anyone explain the differences between pointer variable and
reference variables ?
It is useless since "reference variables" is not C.
Jun 27 '08 #7
On Jun 7, 2:19 pm, "Malcolm McLean" <regniz...@btinternet.comwrote:
References exist in Java and C++, and are basically syntactic sugar for
pointers. They point only to one object, and may never be null.
A bit off the topic, but references can be null in Java. References in
Java work
like constant pointers in C.

Jun 27 '08 #8
Malcolm McLean wrote:
>
<su***********@gmail.comwrote in message news:
>dear all,
can anyone explain the differences between pointer variable and
reference variables ?
References exist in Java and C++, and are basically syntactic sugar for
pointers. They point only to one object, and may never be null. However
the variable is passed as an address, ie a pointer.
<off-topic>

Dunno about C++, but what you say about Java is wrong on
all three points (or at least on two and a half points, allowing
some wiggle room in interpreting "they" in the second point):

1) A Java "reference" *is* what C calls a "pointer," not a
pointer hiding behind a sugar-coated facade. (Java gives
the programmer little freedom to manipulate its pointers,
but they're indisputably pointers. Note what's thrown for
misuse of a null reference: NullPointerException.)

2) A Java reference value refers to only one object, just as
a C pointer value refers to only one object. A Java
reference variable, just like a C pointer variable, can
refer to different objects at different times.

3) A Java reference value or variable can be null.

</off-topic>

--
Eric Sosman
es*****@ieee-dot-org.invalid
Jun 27 '08 #9
"Eric Sosman" <es*****@ieee-dot-org.invalidwrote in message
Malcolm McLean wrote:
>>
<su***********@gmail.comwrote in message news:
>>dear all,
can anyone explain the differences between pointer variable and
reference variables ?
References exist in Java and C++, and are basically syntactic sugar for
pointers. They point only to one object, and may never be null. However
the variable is passed as an address, ie a pointer.

<off-topic>

Dunno about C++, but what you say about Java is wrong on
all three points (or at least on two and a half points, allowing
some wiggle room in interpreting "they" in the second point):

1) A Java "reference" *is* what C calls a "pointer," not a
pointer hiding behind a sugar-coated facade. (Java gives
the programmer little freedom to manipulate its pointers,
but they're indisputably pointers. Note what's thrown for
misuse of a null reference: NullPointerException.)

2) A Java reference value refers to only one object, just as
a C pointer value refers to only one object. A Java
reference variable, just like a C pointer variable, can
refer to different objects at different times.

3) A Java reference value or variable can be null.

</off-topic>
Java references can be null, fair point. The rest of this post is singularly
useless and unhelpful. Do you appreciate why?

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm
Jun 27 '08 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

34 posts views Thread by wilson | last post: by
7 posts views Thread by Xiaoshen Li | last post: by
51 posts views Thread by Kuku | last post: by
14 posts views Thread by Ian | last post: by
12 posts views Thread by Luca | last post: by
33 posts views Thread by Ney André de Mello Zunino | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.