Differences between a pointer and a reference, C/C++ Programming

Assignment Help:

Differences between a pointer and a reference

1.  A reference must always point to some object where as this restriction is not imposed on a pointer.

e.g.

int *pi = 0;           // pointer can point to no object.

const int &ri = 0      would be converted as ,

int temp = 0;

const int &ri = temp;

2. The assignment of one references with other changes the object being referenced and not the reference itself.

e.g.

 int ival1 = 1000, ival2 = 2000;

 int *pi1 = &ival1, *pi2 = &ival2;

 int &ri1 = ival1, &ri2 = ival2;

pi1 = pi2;

 

  • ival1 remains unchanged but pi1 and pi2 now address the same object ival2.

ri1 = ri2;

  • ival1 becomes 2000. ri1 and ri2 still refer to ival1 and ival2 respectively.

 


Related Discussions:- Differences between a pointer and a reference

Inheritance, example of program to add two number

example of program to add two number

Explain syntax rules for writing constructor functions, Syntax rules for wr...

Syntax rules for writing constructor functions Its name must be similar as that of the class to which it belongs. It is declared with no return type (not even void). Ho

C++, Write C++ code for calculating the time table

Write C++ code for calculating the time table

Pattern, enter height of the shape: 1 3 5 3 1 3 5 7 5 3 5 7 9 7 5 3 5 7...

enter height of the shape: 1 3 5 3 1 3 5 7 5 3 5 7 9 7 5 3 5 7 5 3 1 3 5 3 1

Looping, #questiStarting with a blank solution, write a program to prompt t...

#questiStarting with a blank solution, write a program to prompt the user for an employee number, hourly rate and hours worked. Compute and display the employee number, gross weekl

Char, how many bytes required to char

how many bytes required to char

Addition in a existing c++ unification algorithm, Addition in a existing c+...

Addition in a existing c++ unification algorithm Project Description: I have a existing code for a algorithm and need to add two modules into it, it is a unification algorith

Explain multilevel inheritance, Multilevel Inheritance In multilevel in...

Multilevel Inheritance In multilevel inheritance there is a parent class , from whom we derive another class . now from this derived class we can derive another class and so on

What is default argument, Default argument: When the argument is missin...

Default argument: When the argument is missing then the function will read the default value of the missing argument.  To make use of default argument functionality the argu

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd