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 |
Is there a sort function in c++?
What is exception handling? Does c++ support exception handling?
What are signs of manipulation?
Would you rather wait for quicksort, linear search, or bubble sort on a 200000 element array? (Or go to lunch...) a) Quicksort b) Linear Search c) Bubble Sort
structure that describe a hotel with name, address,rooms and number of rooms
What is OOPs
Write a program to read the data and evaluate the results of the election. Print all output to the screen. Your output should specify: The total number of votes, the number of valid votes and the number of spoilt votes. The winner(s) of the election. Hint: An appropriate search should be used to determine the winner(s). The votes obtained by each candidate sorted in terms of the number of votes obtained. Hint: An appropriate sort should be used to sort the candidate(s). The Source code should be saved as VotingSystem. Project Input: Candidates’ Names and Numbers 2501 Victor Taylor 2502 Denise Duncan 2503 Kamal Ramdhan 2504 Michael Ali 2505 Anisa Sawh 2506 Carol Khan 2507 Gary Owen Votes 3 1 2 5 4 3 5 3 5 3 2 8 1 6 7 7 3 5 6 9 3 4 7 1 2 4 5 5 1 4 0 Project Output: Invalid vote: 8 Invalid vote: 9 Number of voters: 30 Number of valid votes: 28 Number of spoilt votes: 2 The winner(s): 2503 Kamal Ramdhan 2505 Anisa Sawh Candidate Score 2503 Kamal Ramdhan 6 2505 Anisa Sawh 6 2501 Victor Taylor 4 2504 Michael Ali 4 2502 Denise Duncan 3 2507 Gary Owen 3 2506 Carol Khan 2
what is C++ objects?
Is python better than c++?
What is a .lib file in c++?
whats the size of class EXP on 32 bit processor? class EXP { char c1; char c2; int i1; int i2; char *ptr; static int mem; };
Can we have "Virtual Constructors"?