What is the difference between reference type and pointers.
Answers were Sorted based on User's Feedback
Answer / manoj kumar kar
Difference 1>
Reference must point to valid objects at the time of
declaration where pointer need not point to valid objects
at the time of declaration means
int nvalue=5;
int &rnvalue; //This is invalid.
int &rnvalue=nvalue; //This is valid.
But
int *rnvalue; //This is valid.
rnvalue=&nvalue;
Difference 2>
Pointer is a variable which holds the address of another
variable.
But Reference is another name of the same variable.
Is This Answer Correct ? | 18 Yes | 1 No |
Answer / k govind
In addition to the previous answer given in Answer #1,
namely References must point to valid objects at the time
of declaration, references also has the following
limitation.
Once a reference is assigned, there's no way you can modify
the reference. However for a pointer type, variable
assignment is legal.
e.g.,
int i, j;
int *pi, *pj;
pi = &i; // pointer to i
pj = &j; // pointer to j
int &k = i; // reference to i
pi = pj; // pi no longer points to i, instead
// it is now pointing to j
k = j; // The reference k is still with i, it is only
// the value of i that is now modified. i is
// assigned the value of j
Is This Answer Correct ? | 3 Yes | 0 No |
Answer / p.madhupriya
pointer is a variable that points to address of another
variable .
reference is variable that points to address of that
variable only.
Is This Answer Correct ? | 2 Yes | 0 No |
Answer / snigdhadeb
REFERENCE:-
#include<iostream.h>
int main()
{
int a=3;
/*A reference variable must be initialized at the time
of it's declaration*/
int &ra=a;
int y=6;
/*int &ry;
ry=y; // not allowed */
int &ry=y;
/* It should be noted that a reference variable dose not
creat a copy so it dose not takes any additional memory
space. Thus memory space is conserved*/
/* It has notational clearness*/
int z=ra * ry;
/* To access the value of a variable thought it's
reference no additional symbol is required, i.e.
dereference is not required*/
return 0;
}
POINTER:-
include<iostream.h>
int main()
{
int a=3;
/* A pointer variable may be declared without
initialization*/
int *pt;
/* A pointer variable may be intialized later on*/
pa=&a;
/* Also a pointer variable may be intialized at the time
of int's declaration*/
int b=5;
int *pb=&b;
/* It is also observed that each pointer variable
required it's own storage, so memory is not conserved*/
/* it's dose not have notational clearness*/
int c=*pa * *pb;
/* Access the value of a variable through it's pointer
requires value at (*) symbol, i.e. dereference is required*/
return 0;
}
Is This Answer Correct ? | 0 Yes | 0 No |
How many types of comments are there in c++?
Find the second maximum in an array?
What is c++ w3school?
Difference between a copy constructor and an assignment operator.
How do you import payscale data from non SAP to SAP?is it through LSMW or any other way is there?
Is c++ pass by reference or value?
How are pointers type-cast?
Write a Program for find and replace a character in a string.
Explain the difference between static and dynamic binding of functions?
Why struct is used in c++?
What does catch(…) mean?
How does a C++ structure differ from a C++ class?