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

What is the maximum number of constructors, Question: (a) (i) Explain...

Question: (a) (i) Explain how class members can be accessed when using objects of the class. Use a suitable example to illustrate your answer. (ii) Explain how the privat

Programming, hello i have about 15 hours the dead line I Need a program in...

hello i have about 15 hours the dead line I Need a program in c language that''s determine the router ip or the gateway ip of the current NIC example the router ip is: 192.168.1.1

Produce data from fixed ocr areas scanned, Produce data from fixed OCR area...

Produce data from fixed OCR areas scanned. Project Description: Project is to design software which can use character recognition only on certain areas of scanned puff's/ jpe

Area under curve, Write a program to find the area under the curve y = f(x)...

Write a program to find the area under the curve y = f(x) between x = a and x = b, integrate y = f(x) between the limits of a and b. The area under a curve between two points can b

#accept 3 digit number, Write a ''C'' program to accept any 3 digit integer...

Write a ''C'' program to accept any 3 digit integer number from the keyboard and display the word equivalent representation of the given number

Define types of storage classes in c language, Define Types of storage clas...

Define Types of storage classes in C language? There are four storage classes in C language: 1. Automatic storage class. 2. External storage class. 3. Register storage cla

Dynamic memory management, C and C++ require explicit dynamic memory manage...

C and C++ require explicit dynamic memory management, using new and delete or malloc() and free(). It is helpful to understand where variables exist (usually the stack or the he

Program to calculate students marks, #include using namespace std; void...

#include using namespace std; void print(int marks_arr[],int cnt) { int ind[cnt]; int i=0; int j=0; int k=0; int s=0; for(k=0;k { ind[k]=0; } int cnt1=0; for(i=0;i

Visual c++, i want write visuaL c++ FOR MY ALGORITHM

i want write visuaL c++ FOR MY ALGORITHM

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