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 |
Differentiate between a pointer and a reference with respect to c++.
How is c++ different from java?
What is a concrete class?
How can you say that a template is better than a base class?
const char * char * const What is the differnce between the above two?
What is the difference between global variables and static varables?
Can you use the function fprintf() to display the output on the screen?
What are disadvantages of pointers?
Write about the retrieval of n number of objects during the process of delete[]p?
catch(exception &e) { . . . } Referring to the sample code above, which one of the following lines of code produces a written description of the type of exception that "e" refers to? a) cout << e.type(); b) cout << e.name(); c) cout << typeid(e).name(); d) cout << e.what(); e) cout << e;
Is c++ free?
What is increment operator in c++?