Pointer Variable





Pointer Variables

 

The variable that is used to hold the memory address of another variable is called a pointer variable or simply a pointer.

 

The data type of the variable (whose address a pointer is to hold) and the pointer variable must be the same. A pointer variable is declared by placing an asterisk (*) after data type or before the variable name in the data type statement.

 

For example, if a pointer variable “p” is to hold memory address of an integer variable, it is declared as:

 

int* p;

 

Similarly, if a pointer variable “rep” is to hold memory address of a floating-point variable, it is declared as:

 

float* rep;

 

The above statements indicate that both “p” and “rep” variables are pointer variables and they can hold memory addresses of integer and floating point variables respectively.

Although the asterisk is written after the data type, it is usually more convenient to place the asterisk before the pointer variable. In this way, a variable with an asterisk is readily recognized as a pointer variable as shown below:

 

float *rep;

 

PROGRAM

 

Write a program to assign two values to two integer variables a & b. Assign the memory addresses of variables a & h to pointer variables x & y, respectively. Print out the memory addresses of variables a & b through their pointer variables.

 

#include <iostreani.h>

main()

{

int a, b;

int *x, *y;

a=126;

b=19;

x = &a;

y= &b;

cout<<”Memory address of variable a =“<<x<<endl;

cout<<”Mernory address of variable b =“<<y<<endl;

}

 

A pointer variable can also be used to access data of memory location to which it points.

 

In the above program x and y are two pointer variables. They hold memory addresses of variables a and b. To access the contents of the memory addresses of a pointer variable, an asterisk (*) is used before the pointer variable.

 

For example, to access the contents of a & b through pointer variables x & y, an asterisk is used before pointer variables as shown below:

 

cout<< “Value in memory address x =” << *x <<end1;

cout<< “Value in memory address y =” << *y <<endl;

 

 

PROGRAM

 

Write a program to assign a value to a variable using its pointer variable Print out the value using the variable name and also print out the memory address of the variable using the pointer variable.

 

#include <iostream.h>

main()

{

int *p;

int  a;

p = &a;

cout<< “Enter data value?”;

cin >> *p;

cout<< “ Value of variable             = “<< a << endl;

cout<< “ Memory address of variable = “<< p <<endl;

 

 

The “void” Type Pointers

 

Usually the type of a variable and the type of pointer variable that holds memory address of the variable must be the same. But the “void” type pointer variables can hold memory address of variables of any data type. A void type pointer is declared by using the keyword “void”. The asterisk is used before pointer variable name.

 

Syntax for declaring void type pointer variables is:

 

void *p;

 

The pointer variable “p” can hold the memory address of variables of any data type.

Other Topics



Comments are closed.

Translator

    Translate to:

Get Adobe Flash playerPlugin by wpburn.com wordpress themes