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

Which language is best, C, C++, Python or Java?
If you want to learn any languages C#, Java, ect. then must to learn C, C++ language as these are base or language to be strong in language skills. C++ is improved version of C language. C#, Java, J# and etc. which are improved version of C++.
Sribaghya
0 0
6
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 an object?
variable created for the data type class
Damanpreet
1 0
5
What is the actual size of INT in the C language, 2 or 4 bytes?
The actual size to int is determined by the compiler as the program runs. But theoretically the size is 2 bytes. You can increase the size by adding keyword long infront of it to make the size 4 bytes. Eg int a; // 2 bytes Long int b; // 4 bytes
Kunal
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

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

Ask a Question

Related Lessons

Variables and Storage Classes
Variables and Storage Classes auto default variable type local to a function static retains the values between function calls initialized only once unavailable outside the scope of the function...

File Handling in C++ : Lesson 1 Data Persistence
Data persistence refers to the existence of data in the program. In other words, it means the life of data. Data is the most significant part of a program and it must be stored properly and easily retrieved...
J

Array vs Linked List
Array Linked List Accessing element is easy. Accessing element is difficult compare to Array. Easy to use. Difficult to use. Memory is Fixed size. Memory is variable size. If...

Do You Know How Is Size Of Structure Defined?
Size of the structure is defined based on multiplies of bigger data type member in the structure. Example: If a structure contains integer, char, short data type, then size of the Structure will be multiples...

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 >

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