473,664 Members | 3,001 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

pointer variable vs reference variables

dear all,
can anyone explain the differences between pointer variable and
reference variables ?
Jun 27 '08 #1
9 2326
su***********@g mail.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.programmin g might be a better place to post this question.

Jun 27 '08 #2
On Jun 7, 8:25*am, sulekhaswe...@g mail.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.c omwrote:
* * 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***********@g mail.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...@btin ternet.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: NullPointerExce ption.)

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.invalidwrot e 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: NullPointerExce ption.)

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
2764
by: Mon | last post by:
I am in the process of reorganizing my code and came across and I came across a problem, as described in the subject line of this posting. I have many classes that have instances of other classes as member variables. So including a forward declaration doesnt help, does it? Faced with these, I had the following options: -Include the appropriate header in the header file that contains the class definition that has a member variable that is...
34
440
by: wilson | last post by:
Hi All, I am a novice at C and just have learned pointer for a short period. Today one error had occured while executing my program below. And I cannot find the error out since it's checked "OK" by Dev C++. Here is:(I want to exchange the value of "a" and "b" with function "swap") void swap(int *pa, int *pb) {
18
3523
by: man | last post by:
can any one please tell me what is the diff between pointer and reference.....and which one is better to use ....and why???????
7
2947
by: Xiaoshen Li | last post by:
Dear All, I thought I understood using pointer variables as function parameters. But I failed to understand why it is needed pass-by-reference of a pointer variable. To me, pointer variable is address variable, which holds the memory address of the object. Using a pointer variable as a function parameter, the function has the ability to CHANGE the object value pointed by the pointer. Why needs pass by reference?
51
4434
by: Kuku | last post by:
What is the difference between a reference and a pointer?
14
7173
by: Vols | last post by:
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
14
18194
by: Ian | last post by:
I am looking at porting code from a C++ application to C#. This requires implementing data sharing functionality similar to what is provided by a smart pointer in C++. I have only recently begun to work in C# and am asking for suggestions/comments of how to implement a similar data sharing technique in C#. A C++ smart pointer can be used to share common information. For example, assume information managed by objects I1, I2, I3,...
12
2426
by: Luca | last post by:
I have 2 questions: 1. Which one is the best: Returning member variables of the class by const ref or returning by pointer? e.g.: Employee* GetEmployee() const; Employee& GetEmployee() const;
33
5049
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the actual counting. Here is the latter's definition: // --- Begin ReferenceCountable.h ---------- class ReferenceCountable
0
8348
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
8861
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...
1
8549
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8636
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6187
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
5660
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
4185
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
2764
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
2
1759
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.