Quick links
Latest articles
Internship
Members
New...
Algorithms
Discrete Mathematics
Big data
Languages
C
C++
C++ STL
Java
Data Structure
C#.Net
Android
Kotlin
SQL
Web
PHP
Python
JavaScript
CSS
Ajax
Node.js
Web prog.
Programs
C
C++
DS
Java
C#
Python
Aptitude
C
C++
Java
DBMS
Interview
C
Embedded C
Java
SEO
HR
CS Subjects
CS Basics
O.S.
Networks
DBMS
Embedded Systems
Cloud Computing
Machine learning
CS Organizations
Linux
DOS
More...
Articles
Puzzles
News/Updates

Home » C++ programming language

What are References and how it is different from pointers?



Learn: The difference between references [preferably used in C++] and pointers [preferably used in C/C++] and would ultimately help in answering a C++ interview question.
Submitted by Deepak Dutt Mishra, on April 30, 2018

This article consists of the description of both references [in C++] and pointers [preferred in C/C++]. The description would finally lead to a conclusion which will draw a difference line between pointers and reference. This article does not conclude that one of these should not be preferred. It will just simply provide you some basic overview over the topics.

References in C++

A reference variable is basically nothing but an alias name or in simpler terms another name declared for a pre-existing variable. For the purpose of declaring a reference or an alias the address-of (&) is used. This operator placed before the variable in declaration statement. After a reference is declared then that variable can be called by either using the original variable name or the alias name given to that variable using & operator.

In simpler words, let a variable name say int a=10; be a variable declared. This variable is a label marking a memory location. Similarly, when we declared a reference say int &r=a; this is yet another label marking that same memory location so we can access that memory by calling any of these two variable names.

References are similar to pointers or in simpler way of understanding they are weak pointers basically developed for the purpose of is in functions as they act as formal parameters[variables declared inside the function declaration parenthesis] in various functions to support reference passing in functions. When a reference is passed into a function, the function works on the original copy of the variable instead of duplicate values as in value passing functions. This enables us to show the operations performed inside the function in the outside of the function. Also references are used for function arguments and return values.



C++ implementation of References

#include <iostream>
using namespace std;

int main()
{
	int i;
	double d;

	int &a=i;
	double &b=d;

	i=20;
	d=0.97;

	cout<<"Value of original  int variable i:"<<i<<endl;
	cout<<"Value of reference int variable a:"<<a<<endl;

	cout<<"Value of original  double variable d:"<<d<<endl;
	cout<<"Value of reference  double variable b:"<<b<<endl;
	
	return 0;
}

Output

    Value of original  int variable i:20
    Value of reference int variable a:20
    Value of original  double variable d:0.97
    Value of reference  double variable b:0.97

There are two basic concepts which a C++ programmer should have knowledge on the topic of references:

a) References as Parameters

This is how a call by reference function works using references.



Example:

#include <iostream>
using namespace std;

/* Here &a,&b are references 
which are used as formal paramater*/
void swap(int&a, int&b)  
{
	int t;
	t=a;
	a=b;
	b=t;
}

int main()
{
	//Here x,y are local variables
	int x=100;     
	int y=200;
	
	cout<<"Initial value of x:"<<x<<endl;
	cout<<"Initial value of y:"<<y<<endl;
	
	//Calling the function swap
	swap(x,y);     
	
	cout<<"Value of x after swapping:"<<x<<endl;
	cout<<"Value of y after swapping:"<<y<<endl;
	
	return 0;
}

Output

    Initial value of x:100
    Initial value of y:200
    Value of x after swapping:200
    Value of y after swapping:100

b) References as Return Values

A C++ program can also return a reference like it returns a pointer.



Example

#include <iostream>
#include <ctime>
using namespace std;
double v[]={11.5,20.1,33.9,45.6,50.2};

double &setValue(int i)
{
	// Returns reference to variable i
	return v[i];      
}

int main()
{
	cout<<"Initial values of v:"<<endl;
	for(int i=0;i<5;i++)
	{
		cout<<"v["<<i<<"] =";
		cout<<v[i]<<endl;
	}

	setValue(2)=12.9;   //Changes 3rd value
	setValue(4)=6.7;    //Changes 5th value

	cout<<"v after change:"<<endl;
	for(int i=0;i<5;i++)
	{
		cout<<"v["<<i<<"] =";
		cout<<v[i]<<endl;
	}
	
	return 0;
	
}

Output

    Initial values of v:
    v[0] =11.5
    v[1] =20.1
    v[2] =33.9
    v[3] =45.6
    v[4] =50.2
    v after change:
    v[0] =11.5
    v[1] =20.1
    v[2] =12.9
    v[3] =45.6
    v[4] =6.7





Quick links:
C FAQ(s) C Advance programs C/C++ Tips & Tricks Puzzles JavaScript CSS Python Linux Commands PHP Android Articles More...

Featured post:
Introduction to Linux (Its modes, Safety, Most popular Applications)
Linux Best Distribution Software (Distros) of 2018

Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.

Comments and Discussions



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com (2015-2018), Some rights reserved.