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

What is the main difference between returning value of a function and does not return any value of a function in  C. Which is correct void main(void), int main(void), int main(int) , or void main(int) and explain the main difference between int main() and int main(void). 

A function may or may not return a value to the calling function. If the function is returning a value, then you have to mention the data type in the function definition and funtion declaration (like int,...
Mahesh Kondawar
What is Enum
An enumeration is a set of named integer constants. An enumerated type is declared using the enum keyword. C# enumerations are value data type. In other words, enumeration contains its own values and cannot inherit or cannot pass inheritance.
S
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
Is it possible to write big safe programs in C++?
yes. C++ is generally used to write system software. For example windows operating system are writeen in C++. Most of the operating system, device driver and antivirus programs are written by using C++.
Sapna
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

Related Lessons

Memory Layout of C Programs
A typical memory representation of C program consists of following sections. Text Segment: A text segment, also known as a code segment or simply as text, is one of the sections of a program in an object...

Copy-on-Write
Note: You can read the original write-up at http://som-itsolutions.blogspot.in/2017/01/copy-on-write.html As i was trying to recapitulate several important concepts of OOAD, naturally COW or Copy-On-Write...

Advantages of C++ Language
Advantages of C++ - C++ is a profoundly convenient dialect and is frequently the dialect of decision for multi-gadget, multi-stage application advancement. - C++ is a protest situated programming dialect...

Definite book guides for C++
This post attempts to collect the few pearls among the dozens of bad C++ books. Unlike many other programming languages, which are often picked up on the go from tutorials found on the internet, only...

Tress And Its Traversal
Depth First Traversals:(a) Inorder (Left, Root, Right) : 4 2 5 1 3(b) Preorder (Root, Left, Right) : 1 2 4 5 3(c) Postorder (Left, Right, Root) : 4 5 2 3 1 Trees are one of the data structures like...

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 >

Almost all of us, inside the pocket, bag or on the table have a mobile phone, out of which 90% of us have a smartphone. The technology is advancing rapidly. When it comes to mobile phones, people today want much more than just making phone calls and playing games on the go. People now want instant access to all their business...

Read full article >

Information technology consultancy or Information technology consulting is a specialized field in which one can set their focus on providing advisory services to business firms on finding ways to use innovations in information technology to further their business and meet the objectives of the business. Not only does...

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 >

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