Reference and Pointer

0 views
Skip to first unread message

Ravina

unread,
Aug 10, 2010, 1:06:47 PM8/10/10
to Knowledge Test
What is the difference between a pointer and a reference?

rajas

unread,
Aug 11, 2010, 8:55:56 AM8/11/10
to knowledge-...@googlegroups.com
a reference must always refer to some object & therefore must be initialized.
pointers do not have such restrictions, a pointer can be reassigned to point to different
object.
 the key difference between pointers & references is that there is no such thing like NULL REFERENCE. A reference is always identifies a valid object in the memory....

is that right???





















TheSuyog

unread,
Aug 11, 2010, 1:58:44 PM8/11/10
to Knowledge Test
A reference is no special variable, its just another name for any
variable it points to. While a pointer is a separate variable itself
which also points to another variable of same type the difference
being that once initialized a reference will also be an alias for the
same variable even if it is forced to pointed to some other variable
which is not d case with pointers.

Ex.:

void main()
{
int a;
int* p;
int& r = a; // Reference var. must
be initialized wen it is declarred.
p = &a;
cout<<&a<<" "<<&r<<" "<<p; // All display the same output.
cout<<"\n"<<&p; // Returns the address of p.
int b;
r = b; // Code has no
effect.
p = &b; // p now points to
b...
cout<<&b<<" "<<&r<<" "<<p; // &b == p != &r...
cout<<"\n"<<&p;
}

A reference and the variable it points to, share the same address
space; while the pointer has its own separate memory space which holds
wat u know...

Important: The reference variable must be initialized with its
declaration itself.

Ravina

unread,
Aug 14, 2010, 7:16:19 AM8/14/10
to Knowledge Test


gud answers guys no need 4 my explanation
Reply all
Reply to author
Forward
0 new messages