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 |
Where the memory to the static variables is allocated?
Define stacks. Provide an example where they are useful.
what Is DCS ? what i will get benefit when i did?
Can you pass an array to a function in c++?
What is the most common mistake on c++ and oo projects?
If you want to share several functions or variables in several files maitaining the consistency how would you share it?
What are the types of STL containers?
Define upcasting.
check whether a no is prime or not.
Explain what is class definition in c++ ?
How do you know that your class needs a virtual destructor?
What does override mean in c++?