Click here to Skip to main content
15,041,025 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
can anyone tell me,
"What is the difference between a C++ pointer and a reference?"
Please explain me it with example :doh: .


[edit]rjm: removed homework and GoogleIt tags; I think this is a perfectly valid question.[/edit]
Posted
Updated 25-May-10 5:27am
v3

A pointer takes the address of an item and remains pointing at that same location until explicitly changed. A reference is more like an alias of the item, it behaves as if it is the item. If the item changes the reference changes and vice versa. Consider the following code:
char string[22];
char *ptr = string;    // ptr points to start of string
char *ptr2 = ptr;      // ptr2 points to start of string
char *&ref = ptr;      // ref refers to same place as ptr
ptr +=3;               // ptr now points 3 characters on
                       // at this point ref also points 3 on, but ptr2 still points at the beginning of string
ref -= 3;              // ref points back at the beginning
                       // and so does ptr

See also Reference Operator[^] on MSDN.
   
Comments
Nemanja Trifunovic 25-May-10 11:58am
   
This is a correct answer. References are in practice implemented as pointers, but that is just an implementation detail. A reference is really an alias.
As I stated in my answer
Hello,


Pointer is a variable which is taking memory reference of the other variable. It will not store direct value of any variable instead it will store memory address of the particular variable.

Reference is taking address of memory storage of particular variable.

int *p; -- here, p is a pointer variable.

int c = 5; -- here, c is a normal variable.

p = &c; -- here, p is taking memory reference of c.

print("%d",&p); --here, it will display memory address of c;

Also, please find below important links -

http://cplus.about.com/od/learningc/ss/pointers_9.htm[^]

http://www.cplusplus.com/doc/tutorial/pointers/[^]

http://augustcouncil.com/~tgibson/tutorial/ptr.html[^]


Hope, this will clear your doubt.

Regards,

Nilesh Shah.
   
v2
Comments
Richard MacCutchan 25-May-10 11:21am
   
Reference and AddressOf are separate and distinct, see the link in my answer.
Aescleal 25-May-10 15:11pm
   
Reason for my vote of 1
A reference is nothing to do with addresses, it's just an alias for another variable.
As CPallini said, for the actual implementation, the compiler substitutes a reference with a pointer.
This is only a syntax thing.
You can see this if you take the assembly output of a program.

Here is a link where the man himself speaks about why references were added to C++ - Why does C++ have both pointers and references?[^]
   
A C++ reference is a const pointer with some added syntactic sugar.

try this sample code:
C++
#include <iostream>
using namespace std;
void byval(int x)
{
  x += 5;
}
void byref(int & x)
{
  x += 5;
}
void bypointer(int * px)
{
  *px +=5;
  px++;
  *px +=5;
}
void main()
{
  int a[2];
  char c;
  a[0] = 0; a[1]=1000;
  byval(a[0]);
  byval(a[1]);
  cout << "byval " << a[0] << ", " << a[1] << endl;
  a[0] = 0; a[1]=1000;
  byref(a[0]);
  byref(a[1]);
  cout << "byref " << a[0] << ", " << a[1] << endl;
  a[0] = 0; a[1]=1000;
  bypointer(&a[0]);
  cout << "bypointer " << a[0] << ", " << a[1] << endl;
  cin >> c;
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900