Exforsys.com
 

Sponsored Links

 

C++ Tutorials

 
Home Tutorials C++
 

C++ Dereference Operator

 
Category: C++
Comments (2)

C++ Dereference Operator

In this C++ tutorial, you will learn how to access the value of variables pointed by the pointer variables using pointer concept discussed in detail.



It is possible to access the value of variables pointed by the pointer variables using pointer. This is performed by using the Dereference operator in C++ which has the notation *.


The general syntax of the Dereference operator is as follows:


*pointer_variable


In this example, pointer variable denotes the variable defined as pointer. The * placed before the pointer_variable denotes the value pointed by the pointer_variable.


For example:


exforsys = 100;
test = exforsys;
x = &exforsys;
y=*x;


In the above example, the assignment takes place as below:



That is



In the above example, the exforsys is a integer variable having the value of 100 stored in memory address location 3501.


The variable exforsys is assigned to the variable test in the second statement:


test = exforsys;


The value of the variable exforsys is 100 and is then copied to the variable test.


In the third statement, the address of the variable exforsys denoted by reference operator &exforsys is assigned to the variable x as:


x = &exforsys;


The address of the variable 3501 and not the contents of the variable exforsys is then copied into the variable x.


The fourth statement makes use of the deference operator:


y=*x


This means that the value pointed to by the pointer variable x gives the value 100 to y.


Some points for the programmer to note:


  • The programmer must note that the x refers to the address 3501 whereas *x refers to the value stored in the address 3501 namely 100.
    .
  • The reference operator is denoted by & and deference operator denoted by * . Both differ in their meaning and functionality. The reference operator denotes the address of. The dereference operator denotes the value pointed by. In short, a deference variable can be denoted as referenced.
    .
  • If the programmer wants to define more than two pointer variables, then comma operator may be used in this instance. The programmer must carefully place pointer symbol * before each pointer variable.

For instance, if the user wishes to define two integer pointer variables, e1 and e2, this can be done as follows:


int *e1,*e2;


If the programmer declares:


int *e1,e2;


This means that e1 is a pointer variable pointing to integer data type but e2 is only an integer data type and not a pointer type. The programmer must ensure to place * before each pointer variable.


The dereferencing or indirect addressing is performed using the indirection operator * used to access the value stored in an address.


The defining of pointer variable:


int* exf;


The definition of pointer variable as in the above case is the pointer variable exf.


It is also possible to assign value to pointer variable using indirection operator. This gives the same effect as working with variables.


For Example:



#include
void main()
{
int example,test;
int* exforsys;
exforsys=&example;
example=200;
test=200;
*exforsys=100;
test=*exforsys;
cout< cout< }


The output of the above program is


100
100


In the above example, the pointer variable exforsys points to integer variable example and takes the address of the variable example as:



test and example have initial values of 200.


The statement:


*exforsys=100


Sets the value of the variable pointed by pointer variable exforsys as 100. This is equivalent to setting example=100.


The assignment is as follows:


example test



The statement


test=*exforsys;


sets the value of variable test with the value pointed by the pointer variable exforsys which is 100 as seen in the above example. Test also becomes 100 and the assignment becomes:



Both results are displayed having the value 100.



Defining pointers and using them to access the variable pointed by them is an important concept thoroughly detailed in this tutorial. In the next section, the powerful concept of pointers with other features of C++ such as arrays and functions will be explained in detail.



Read Next: Object Oriented Programming Paradigm

 

 

Comments


eswaraiah.j said:

  while i am reading the figures make me understood more clear the concepts.
i am happy to read this and learn the concept.
thanking you .
my email id: eswaraiah_j@yahoo.co.in
November 8, 2007, 5:08 am

Prarthana said:

  thanks for clarifying the concept
January 7, 2010, 8:34 am

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2010 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape