UrbanPro

Learn C++ Language from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Why c++ introduced reference variable?

Asked by Last Modified  

Follow 0
Answer

Please enter your answer

MS SQL SERVER DBA Trainer

C++ references allow you to create a second name for the a variable that you can use to read or modify the original data stored in that variable.
Comments

C++, C, Embedded, Linux Expert

(A)It makes the syntax more clear and increase the readability of program. E.g: void change(int* a, int* b) (a) // in case of pointer void change (int& a, int& b ) (b) // in case of reference Int main() { int i = 0, int j = 0; change (&i, &j); // in case of pointer change (I, j); //...
read more
(A)It makes the syntax more clear and increase the readability of program. E.g: void change(int* a, int* b) (a) // in case of pointer void change (int& a, int& b ) (b) // in case of reference Int main() { int i = 0, int j = 0; change (&i, &j); // in case of pointer change (I, j); // in case of reference : you can see the difference, it is just like a simple function call } (B) When we use reference, copy can be avoidable which saves the memory. Main difference between pointer and reference are as follows: 1- Pointer can be NULL but reference cannot be. 2- Pointer can hold the address of variables while reference holds the address of duplicate of that variable. 3- Reference must be initializing at the time of declaration but this not the case with pointer. 4- There is no need to dereference the reference but a pointer to be dereferenced. 5- Arithmetic operation cannot be perform on reference whereas can do with pointers. read less
Comments

Expert C/C++ Trainer with over 15 years of Experience

References are alternative of pointers. It is simpler to use references over pointers. Internally references are similar to pointers. Eg: Indirect access using pointers. int x =10; int *ptr = &x; //modify value of x indirectly *ptr = 20; //increment by 1 *ptr++ is wrong (*ptr)++ is correct. Reference int...
read more
References are alternative of pointers. It is simpler to use references over pointers. Internally references are similar to pointers. Eg: Indirect access using pointers. int x =10; int *ptr = &x; //modify value of x indirectly *ptr = 20; //increment by 1 *ptr++ is wrong (*ptr)++ is correct. Reference int &ref=x; ref=20; ref++; Late binding and Run-time polymorphism can be achieved by reference and pointer. I hope u got the basic point. Regard Syed read less
Comments

First we need to understand the difference between the pointers (used in C) and reference variables( used in C++). 1. Pointer is a variable which holds the address of some other variable. 1. In C, pointers were introduced to manipulate the data stored at a particular memory location. 2. It was...
read more
First we need to understand the difference between the pointers (used in C) and reference variables( used in C++). 1. Pointer is a variable which holds the address of some other variable. 1. In C, pointers were introduced to manipulate the data stored at a particular memory location. 2. It was easier to access, store and manipulate the data using pointers e.g. 2d or 3d matrices, or accessing data using pointers in functions, accessing data stored in structures. 3. But pointer could create lot of bugs accidentally (typing mistake like using *(p+1) instead of *(*p+1) or deliberately (to create viruses etc). In C++ and Java, pointers were totally removed to avoid above mentioned issues (bugs, viruses and complexity). 1. Reference variable is another name (alias) given to the same memory location. 2. They work similar to pointers but are easy to use and less complicated. 3. They must be initialized at the time of declaration, unlike pointers in C. (undeclared pointers in C can point anywhere in the memory). 4. In C++ functions, no address operator(&) is used at the time of function call, unlike pointers where (&) operator is used at the time of function call. 5. As 80% of C syntax can be used in C++, pointers can be used in C++ programs also. read less
Comments

C++ references allow you to create a second name for the a variable that you can use to read or modify the original data stored in that variable. While this may not sound appealing at first, what this means is that when you declare a reference and assign it a variable, it will allow you to treat the...
read more
C++ references allow you to create a second name for the a variable that you can use to read or modify the original data stored in that variable. While this may not sound appealing at first, what this means is that when you declare a reference and assign it a variable, it will allow you to treat the reference exactly as though it were the original variable for the purpose of accessing and modifying the value of the original variable--even if the second name (the reference) is located within a different scope. This means, for instance, that if you make your function arguments references, and you will effectively have a way to change the original data passed into the function. This is quite different from how C++ normally works, where you have arguments to a function copied into new variables. It also allows you to dramatically reduce the amount of copying that takes place behind the scenes, both with functions and in other areas of C++, like catch clauses. read less
Comments

Programming in C, C++, Unix

It gives a java style of easier coding, whrein, data can be passed by reference without using complexity of pointer. But so many features still mandate the use of pointers in real life, like *this.
Comments

They allow passing of parameters used for output with no explicit address-taking by the caller. There is no dereferncing implies no confusion
Comments

C,C++17,Data Structure, Algorithm, STL,Multithreading,Async, Assignments - Online tutor

using reference, copy can be avoidable which saves the memory.
Comments

C,C++17,Data Structure, Algorithm, STL,Multithreading,Async, Assignments - Online tutor

While you are passing a big object to a function, it creates a new copy of the same object. So instead of creating a new copy we are just referring the same object. here you will get the benift of using reference.
Comments

Trainer - C# .Net, C++, SQL Server

Depending on the requirement, If user want to change the value of a variable without creating another variable, one can use reference variable.
Comments

View 12 more Answers

Related Questions

Is C/C++ language is still important in 2019?
Of course, C language is only the heart of any processor or controller.Because of complexity in C peoples are looking to work on High-level languages like JAVA etc.
Tejas
0 0
7
What is different between malloc and calloc?
The Diffrences are like-- malloc() takes a single argument (memory required in bytes), while calloc() needs two arguments. Secondly, malloc() does not initialize the memory allocated, while calloc() initializes...
Roopali
How to register as teacher?
On the basis of his total experience and Qualification you can do it.
Shrikrishna
please tell me the best book for c programming language
LET US C by Yashwant Kanetkar. This book is very useful to improve the practical Knowledge. For Theory - Programming in C by Balagurusamy
Sunil
Is C#, C++, Python, or JS better for game development?
All languages good for gamjng. But you can make 3d games in c# uaing unity software and it's very very interesting to work on it.
Mohit
0 0
6

Now ask question in any of the 1000+ Categories, and get Answers from Tutors and Trainers on UrbanPro.com

Ask a Question

Recommended Articles

Introduction C++ is an excellent programming language and many of the applications are written in C++ language. It has generic, object-oriented & imperative programming features, and also provides facilities for low-level memory manipulation. Successor of C language, it is an OOP (object oriented programming) language...

Read full article >

Hadoop is a framework which has been developed for organizing and analysing big chunks of data for a business. Suppose you have a file larger than your system’s storage capacity and you can’t store it. Hadoop helps in storing bigger files than what could be stored on one particular server. You can therefore store very,...

Read full article >

Software Development has been one of the most popular career trends since years. The reason behind this is the fact that software are being used almost everywhere today.  In all of our lives, from the morning’s alarm clock to the coffee maker, car, mobile phone, computer, ATM and in almost everything we use in our daily...

Read full article >

Business Process outsourcing (BPO) services can be considered as a kind of outsourcing which involves subletting of specific functions associated with any business to a third party service provider. BPO is usually administered as a cost-saving procedure for functions which an organization needs but does not rely upon to...

Read full article >

Looking for C++ Language Classes?

Learn from the Best Tutors on UrbanPro

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you
X

Looking for C++ Language Classes?

The best tutors for C++ Language Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn C++ Language with the Best Tutors

The best Tutors for C++ Language Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more