Question

In C++ what is the difference between pointer variable and reference variable? Please give an example.

In C++ what is the difference between pointer variable and reference variable? Please give an example.

Homework Answers

Answer #1

The difference between pointer variable and reference variable are:-

Pointer variable Reference variable
1.) Pointer variable stores address of the variable. Reference variable refers to variable that already exists but with other name.
2.) Pointer variables can assign null value. Reference variable cannot assign null value.
3.) Pointer variable can be referenced by pass by reference. Reference variable can be referenced by pass by value.
4.) Pointer variable may or may not initialized on declaration. Reference variable must initialized on declaration.
5.) Pointer variables are able to have their own address of the memory. Reference variable is used to share the identical address of the memory that too with the initial variable.
6.) Pointer variables are able to have their own size on the stack. Reference variable are able to take same memory on the stack.
7.) Syntax: type *pointer; Syntax: type &newname =existing name;

Example :-

Reference variable in C++:

#include<iostream>
using namespace std;
int main()
{
int n = 40;
int& reference = n;
reference = 50;
cout << "n = " << n << endl ;
n = 60;
cout << "reference = " << reference << endl ;
return 0;
}

Output:

n = 50

reference = 60

Pointer variable in C++:

#include <iostream>
#include <string>
using namespace std;

int main() {
string animal = "Dog";
string* ptr = &animal;
cout << animal << endl;
cout << &animal << endl;
cout << ptr << endl;
return 0;
}

Output:

Dog                                                                                                                                             

0x7fff4a80d6e0                                                                                                                                  

0x7fff4a80d6e0   

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
What's the difference between step-variable cost and step-fixed cost? Please give example for each of them.
What's the difference between step-variable cost and step-fixed cost? Please give example for each of them.
What is the difference between a manager and a leader ? Please give an example of...
What is the difference between a manager and a leader ? Please give an example of a good leader (politician, athlete, celebrity, businessman or woman). What qualities do they possess ( to be an effective leader)??
Please give an example of the difference between a Income statement And Balance sheet
Please give an example of the difference between a Income statement And Balance sheet
What is the difference between a continuous random variable and a discrete random variable? In your...
What is the difference between a continuous random variable and a discrete random variable? In your own words, do not copy from the internet. Please also give an example.
explain the difference between discretionary and automatic spending by the government. Please give an example of...
explain the difference between discretionary and automatic spending by the government. Please give an example of discretionary spending and automatic spending.
Please explain the difference between Experimental and Quasi Experimental Designs. Give an example of one of...
Please explain the difference between Experimental and Quasi Experimental Designs. Give an example of one of the types.
Can someone please explain what the difference is between parameters and variable, for example x+y=6 ax+by=5...
Can someone please explain what the difference is between parameters and variable, for example x+y=6 ax+by=5 a and b are parameters? x and y are variable? or they are the same? How are we supposed to define it?
What is the difference between an interview and a survey? Give an example of either of...
What is the difference between an interview and a survey? Give an example of either of the types of techiques.
What is the difference between an opinion, fact and an assumption? Give an example of an...
What is the difference between an opinion, fact and an assumption? Give an example of an opinion, a fact and an assumption.
Please describe the difference between “pass by value” and “pass by reference” In Scala, please be...
Please describe the difference between “pass by value” and “pass by reference” In Scala, please be Specific.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT