Pointers
Pointers
POINTERS
Whenever we declare a variable, an appropriate memory location is allocated
somewhere in the memory to hold the value of variable.
Consider int s=999;
This statement instructs the system to reserve a memory location for the integer
variable s and to hold the value of 999 in it. Let us assume system has chosen the
address location 5000 for s.
s variable
999 value
5000 Address
Since memory locations are simply numbers, we can assign some variables to store
these address location like any other variable. Such variables that hold memory
addresses are called pointer variables.
What are pointers?
A pointer is a variable whose value is the address of another variable, i.e., direct
address of the memory location.
Like any variable or constant, you must declare a pointer before using it to store any
variable address. The general form of a pointer variable declaration is
datatype *var-name;
* tells the var-name is a pointer variable.
var-name needs a memory location.
var-name points to a variable of type data type.
The * used to declare a pointer is the same asterisk used for multiplication.
Eg: int *p;
Variable p as a pointer variable that points to an integer data type.
float *x;
Variable x as a pointer variable that points to a floating point variable.
int s=963; s ps
1000
963
int *ps;
5000
1000
ps=&s;
s ps
963 8487
Initialization of pointer variables:
The process of assigning the address of a variable to a pointer variable is known as
initialization.
All uninitialized pointers will have some unknown values that will be interpreted as
memory locations. They may not be valid addresses or they point to some values that
are wrong.
Once a pointer is declared, it can initialized as
int quantity;
int *p;
p=&quantity;
Or we can combine like
int *p=&quantity;
Only requirement is quantity has to be declared before initialization.
Accessing a variable through its pointers:
The unary operators & and * are two special pointer operators.
The operator & returns the memory address of its operand. The operation of the &
can be remembered as returning the address of the variable it preceeds.
The operator * is the complement of &. It returns the value of the variable located at
the address that follows. The operation of the * can be remembered as at the address
which follows it. This operator is called indirection operator.
In the declaration * is used to identify the variable ip as pointer variable. The pointer
variable is then initialised with the address of int variable var. In next statement the
operator * is used along with same pointer ip. *ip will return the value at the address
stored in ip.
Usage of pointer operator
usage Meaning
var Value of variable var
&var Address of variable var
*&var Value stored at the address of variable var (equivalent to var)
ip Value of variable ip (since ip is a pointer variable it contains an address.
*ip Value stored at address contained in ip.
&ip Address of variable ip.
&*ip Address of *ip (equivalent to ip)
Pointer expressions
Like any other variable pointer can be used in expressions. For example,
p2=p1;
Y=*p1 * *p2;
sum=sum + *p2;
Z=5* - *p2/ *p1;
The only arithmetic operators that can be used on pointers are addition and
subtraction of integers.
i.e. p1+4, p2-2, p1- p2, p1++, -p2, sum+= *p2 are valid but p1+p2 is not valid.
Pointers can be compared using relational operators.
i.e. p1>p2, p1=p2, p1!=p2.
Pointers cannot be used in division or multiplication.
i.e. p1/p2, p1*p2, p1/3 are not allowed.
A program to illustrate the use of pointers in arithmetic operations.
Pointer increment and scale factor:
Pointers can be incremented as p1=p1+4; & p1=p1+1;
Also expression p1++ will cause pointer to point to the next value of its type.
Eg: if p1 is an integer pointer with an initial value say 2800, then after operation
p1=p1+1, the value of p1 will be 2802 and not 2801. i.e. when we increment a
pointer, its value is increased by the ‘length’ of the data type that it points to. This
length is called scale factor.
For an IBM PC, the length of various data types are as follows.
Characters 1 byte
Integers 2 bytes
Float 4 bytes
Long integers 4 bytes
Double 8 bytes
The number of bytes used to store various data types depends on the system and can
be found by making use of the sizeof operator.
Eg: if x is a variable, then sizeof(x) returns the number of bytes needed for the
variable.
Systems like pentium use 4 bytes for integers and 2 byte for short integers.
NULL POINTER
A pointer that does not point to a valid memory location is known as a NULL
pointer.
A NULL pointer is nothing but a pointer which points to nothing.
NULL is a constant defined in many header files in C programming language
including stdio.h
NULL pointer can be declared as
int *nptr=NULL; or int *nptr=0;
Uses of Null pointer:
1. To initialise a pointer variable when that pointer variable before it is assigned
with any valid address.
2. To pass a null pointer to a function argument when we do not want to pass any
valid memory location.
Qn: Do the following using pointers
1. add two numbers
2. Swap two numbers using a user defined function.
ARRAY ACCESS USING
POINTERS
Once array is declared, the compiler allocates a base address and sufficient amount
of storage space to hold all elements of the array in contiguous memory locations.
The base address is the location of the first element (index 0) of the array.
Eg: int a[4]={2,3,4,5};
Suppose base address is 1000 and assuming each integer requires 2 bytes.
Elements a[0] a[1] a[2] a[3]
Value 2 3 4 5
Address 1000 1002 1004 1006
The name a denotes to a constant pointer points to first element a[0] and therefore
the value of a is 1000, the location where a[0] is stored.
a=&a[0]=1000;
If we declare p as an integer pointer pointing to the array a by the following
statement:
p=a; and this is equivalent to p=&a[0];
Now we can access every value of a using pointer increments. i.e.
p=&a[0]; (i.e. 1000)
p+1=&a[1]; (i.e. 1002)
p+2=&a[2]; (i.e. 1004)
p+3=&a[3]; (i.e. 1006)
Address of a[3]=base address+(3 x scale factor of int)
=1000+(3 x 2)=1006
While handling arrays instead of using array indexing, pointers can be used to access
array elements. i.e. p+3 is equivalent to a[3]. Pointer accessing method is much
faster than array indexing.
int a[4]={2,3,4,5};
int *p;
p=a; or p=&a[0];
p=&a[0] *p=2
*(p+1)=3
p+1=&a[1] *(p+2)=4
p+2=&a[2] *(p+3)=5
p+3=&a[2]
Write a program using pointers to compute the sum of all elements stored in an
array.
A two dimensional array can be considered as an array of number of one
dimensional arrays.
For one dimensional arrays, array name represents the address of its zeroth element.
In two dimensional array also array name will represent the address of its zeroth
element i.e. zeroth row or the zeroth one dimensional array inside it.
Consider a[3][4]
This can be treated as an array consisting of 3 one dimensional arrays. Each these 3
arrays consists of 4 elements each. The address of the zeroth element or zeroth array
present in a[3][4] is given by a or a+0. hence a+i will be the pointer to ith row.
Value of zeroth element in the ith row is given by *(a+i) and pointer to jth element in
ith row is given by *(a+i)+j. So the value stores at a[i][j] is given by *(*(a+i)+j).
p+0 0 1 2 3 4
p+1 1
p+2 2
p+3 3
p+4 4