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

Explain the class invariant, Explain the class invariant. - It's a cond...

Explain the class invariant. - It's a condition that ensures correct working of a class and defines all the valid states for an object. - When an object is created class inv

Is there any difficulty with the following : char*a=null; , Is there any di...

Is there any difficulty with the following : char*a=NULL; char& p = *a;? A: The result is indeterminate. You must never do this. A reference has to always refer to some object.

Array, array itself is a pinter.explain

array itself is a pinter.explain

Graphical user interface designed for the output peripherals, Introduction ...

Introduction to I/O interfacing, discuss microcontrollers applications in industry. Detailed product design specifications that have sections on both hardware and software inclu

Write a program to define a matrix, Write a program to define a matrix: ...

Write a program to define a matrix: 1. Write a function that takes an integer and calculates and returns the factorial of the integer. The Factorial of a number "n" is compute

Programming, what is the first thing I need to know about Programming?

what is the first thing I need to know about Programming?

Reading from a Hash table in C, I''m having trouble with my C code to red f...

I''m having trouble with my C code to red from a Hash table. Not sure what it is I''m doing wrong and stumped. I need to get it figured out by tomorrow at 4:00 p.m. Eastern time. C

Boardcoloring, color representation 0,1,2,3,4,5,6,7,.......

color representation 0,1,2,3,4,5,6,7,.......

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