Pointers

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 21

VKS-LEARNING HUB

TEXT FILE
HANDLING
VKS-LEARNING HUB

In a programming language like C++, it is possible to represent


an integer constant in different form. Generally an integer value
is represented as a Decimal integer. A decimal integer value
consists of any 10 digits (0-9). Integers 29, 73545, 8545, -34,
-428954 and 3945 are example of Decimal integer values.

In C++ it is also possible to represent an integer as a


Hexadecimal integer. A Hexadecimal integer value consists
of 16 digits (0-9, A-F). Integers 2A, 4B6C, ABCD and F16 are
example of Hexadecimal integer constant. In a C++ program
Hexadecimal integer constant is prefixed by 0x. For example
1B4C is a Hexadecimal integer constant but in C++ program
it will be represented as 0x1B4C.
VKS-LEARNING HUB

An example of decimal integer and Hexadecimal integer is


given below:
Variable hi is assigned a Hexadecimal
#include<iostream.h> integer constant while variable di is
void main() assigned Decimal integer constant. First 2
{ outputs display values stored in variables
int hi=0x1B4C; hi and di as Decimal integer. Last 2
outputs display values stored in variables
int di=174911;
hi and di as Hexadecimal integer.

cout<<"Dec="<<hi<<","<<"Dec="<<di<<endl;
cout.setf(ios::hex, ios::basefield);
cout<<"Hex="<<hi<<","<<"Hex="<<di<<endl;
}

Running of the program produces following output:


Dec=6988,Dec=174911
Hex=1B4C,Hex= 2AB3F
VKS-LEARNING HUB

Each variable is assigned a memory slot (the size depends


on the data type) and the variables data is stored there
VKS-LEARNING HUB

Pointer

A variable in C++ has three characteristics


data type of the variable,
value stored in the variable
the address of variable.

a b Variable Names

2014 89.7 Values Stored


Address of the
0012ff88 0012ff80 Variables
VKS-LEARNING HUB

So far in our programming examples we have only


used the first two characteristics, that is, data type of
the variable and the value stored in the variable.
Address of a variable represents the location of the
variable in the computers main storage (RAM

To get an address of a variable we use address


operator (&) before a variable name.

In C++ address of a variable is also known as Pointer.


Pointer (address) is displayed as a Hexadecimal
integer.

An pointer will display address except for a pointer to


a character.
VKS-LEARNING HUB

#include<iostream.h> Variable a is assigned a


void main() value 20 and variable b is
{ assigned a value 88.5.
int a=2014; First two outputs display
double b=89.7; value stored in the variable
a and b.
cout<<"a="<<a<<",b="<<b<<endl; Last two outputs display
cout<<"&a="<<&a<<",&b="<<&b; address of the variables a
} and b. Addresses of the
Running of the program produces variables are displayed as
following output: Hexadecimal integers.
a= 2014 , b= 89.7
&a= 0x0012ff88 , &b= 0x0012ff80
VKS-LEARNING HUB

Pointer Variable
To store an address of a variable we need to create a special
type of variable called Pointer variable. Creating a Pointer
variable is similar to creating a variable of fundamental data
type or array type.

.
VKS-LEARNING HUB

Rule: DataType* PointerVarName;


DataType *PointerVarName;
DataType *PointerVarName1, *PointerVarName1, ;

DataType could be fundamental data type or


derived data type like structure type or class type.
Operator star (*) is needed between DataType and
PointerVarName. Operator star (*) implies that the
variable that is being created is Pointer type. When
using the Pointer variable in the program, operator
star (*) is never used, that is, in the program only
PointerVarName will be used
VKS-LEARNING HUB

Pointer Declaration
Thus, the character * can appear
anywhere between the data type
name and the variable name.
VKS-LEARNING HUB

What is the Pointer Value


VKS-LEARNING HUB

Pointer Declaration
int* p, q;
In this statement:
only p is the pointer variable, not q.
Here, q is an int variable.
we prefer to attach the character * to
the variable name. So the preceding
statement is written as:
int *p, q;
VKS-LEARNING HUB

Pointer Operators
C++ provides two operators operator
to work with pointers.

(&) the address of operator

(*) the dereferencing


VKS-LEARNING HUB

Address of Operator (&)


is a unary operator that returns the
address of its operand.
For example, given the statements:
int x;
int *p;
The statement:
p = &x;
assigns the address of x to p. That is, x
and the value of p refer to the same
memory location
VKS-LEARNING HUB

Dereferencing Operator (*)


referred to as indirection operator
refers to the object to which its
operand (that is, the pointer) points.
For example, given the statements:
p X
2
5
VKS-LEARNING HUB

Dereferencing Operator
(*)
VKS-LEARNING HUB

Dereferencing Operator
(*)
Consider the following statements.

1. num = 78;

2. p = &num;

3. *p = 24;
VKS-LEARNING HUB

Example
#include <iostream.h>
#include <string>
//===============================
void main() {
int *p;
int x = 37;
cout <<"x= " << x<<endl;
p=&x;
cout <<"*p= "<< *p <<" , x= "<<x<<endl;
*p=85;
cout <<"*P= " <<*p <<" , x= "<<x<<endl;
cout <<" Address of p is : "<< &p <<endl;
cout<<"value of p : "<< p<<endl;
cout<< " value of memory location pointed to by *p = "<<*p<<endl;
cout<<"Address of X = " << &x<<endl;
cout <<" Value of x = " << x<<endl;
}
VKS-LEARNING HUB
VKS-LEARNING HUB
VKS-LEARNING HUB

You might also like