INFORMATION FROM DR. EBRAHIMI'S C++ PROGRAMING EASY WAYS

Home                  ABOUT US                    CONTACT US                  TUTORIAL

CHAPTER 13

 

ADDRESS, POINTER VARIABLE,

 DYNAMIC MEMORY ALLOCATION

 

 

A computer’s memory is divided into slots.  Each slot has an address that can hold a value.  A memory that holds a value and has the ability to change it when necessary is known as a variable. There are variables that hold values such as integers, characters, or other data types.  A variable that holds an address of another variable is known as a pointer variable or simply as a pointer.  Pointers provide open access to memory addresses and are beneficial to programmers, but pointers can put the security of a system at risk.  For this reason, pointers are a controversial topic. The storage for pointer variables can be allocated during run time (dynamic allocation), as the program requires the memory.  The memory for a dynamically allocated variable can be freed after its usage.  This allows availability of the storage for other variables. The allocation and de-allocation of memory leads to a great saving of computer memory and a programmer can build their own data structures and manipulate them as desired. Furthermore, this provides other alternatives in building data structures.

 

 

ADDRESS OF A VARIABLE

 

Every variable has an address and a value. Normally when a variable is declared, an address is assigned to the variable by the system (compiler).  The address of a variable is accessed by an ampersand &; it is known as an address operator.

Text Box: #include <iostream>
using namespace std; 
 int main(){
int x;
            cout <<"ADDRESS OF X IS: "<<&x<<endl;
            x=5; 
            cout<<"X IS: "<<x<<endl;      
            return 0;
}//MAIN
 

  

 

 

 

 

 

 

 

 

 

Text Box: ADDRESS OF X IS: 0x74272400
X IS: 5
Text Box: Figure 13.1a – Program to show the address of a variable
Text Box: Figure 13.1b – Output of Figure 13.1a
 

 

 

 

 

 

 


 

A POINTER VARIABLE

A regular variable holds a value such as an integer. A pointer variable, or simply a pointer, holds a value as well but its value is the address of another variable. Through this address, the value of the variable can be accessed. In other words, a pointer can indirectly access the value of the variable. An asterisk (*) before the variable name is used to declare a pointer variable.

 

Text Box: #include <iostream>
using namespace std;
int main(){
            int *x;
            int y;
            x=&y;   
            y=5;
            cout<<"X IS: "<<x<<endl;
            cout<<"THE ADDRESS OF Y IS: "<<&y<<endl;
            cout<<"Y IS: "<<y<<endl;
            cout<<"THE CONTENT OF X IS: "<<*x<<endl;
            return 0;
}//MAIN
 

  

 

 

 

 

 

 


 

 

 

 

 

 

 

 

Text Box: X IS: 0x0065FDF0
THE ADDRESS OF Y IS: 0x0065FDF0
Y IS: 5
THE CONTENT OF X IS: 5
Text Box: Figure 13.2a – Address and content of pointer variable
 

 

 

 

 

 

Text Box: Figure 13.2b – Output of Figure 13.2a

  

 

 

 

 

 


 

DECLARING A POINTER VARIABLE

 

The method used to declare a pointer variable is similar to an ordinary variable except that an asterisk (*) is placed before the variable name. A pointer variable can be declared of a primitive type such as int, char, float, double, as well as other structured types such as an array, structure, or class. In the following example, x is a pointer variable that points to an integer value and, similarly, y is a pointer variable that points to a character value. 


 

 

Text Box: 0x0065FDEC IS ADDRESS OF A CONTAINING INTEGER: 5
m¦¦¦ IS ADDRESS OF B CONTAINING CHARACTER: m
Text Box: Figure 13.3a – Declaration of a pointer
Text Box: #include <iostream>
using namespace std;
int main(){
int *x;  
char *y;
int A=5;
char B='m';
x=&A;
y=&B;
cout<<x<<" is address of A containing integer: "<<A<<endl; 
cout<<y<<" is address of B containing character: "<<B<<endl;
return 0;
}//MAIN

  

 

 

 

 

 

 

 

 

 

 

 

 

 

 


 

                                                                                                                                 

 

Text Box: Figure 13.3b – Output of Figure 13.3a

  

 

 


 

ACCESSING THE CONTENT OF WHAT A POINTER POINTS TO: INDIRECTION OPERATOR

 

Accessing the content of what a pointer points to is similar to the way the value of a regular variable is accessed with one exception:  the usage of an asterisk before the pointer variable. This indirect access of the variable value is known as indirection and the asterisk (*) before the variable name is known as the indirection operator or de-reference operator.

 

Hosted by www.Geocities.ws

1