You are on page 1of 8

5 Pointers PIC-17212

Chapter 5: Pointers
Understanding pointers, declaring pointer variable, initialization of pointer variable, accessing address of
a variable, pointer expressions, Pointers arithmetic (12 Marks)

Pointer variables:
Pointers are variables used to store memory addresses of the normal variables. Normal variables
store values while pointer variables store the addresses of these variables. When we declare some
variable, memory space is allocated to it. Each location in memory has its own address. One byte of
data is stored in one memory location. To store integer values 2 bytes of memory is used, for floating
point variables 4 bytes, for double type variables 8 bytes and for character (char) variables, 1 byte of
memory is allocated.
Consider the following declaration:
int i = 3;
This declaration informs the C compiler to:
1) Reserve space in memory to hold the integer value.
2) Give the name i to this memory location.
3) Store the value 3 at this location.
Following figure explains this:

i name of memory location


3 value stored at location

14620 address of the memory location


A pointer variable can be declared to store the address of location i as follows:
int *ptr;
int i = 3;
ptr = &i;

ptr i
14620 3

14620

1
Computer Department, Jamia Polytechnic (0366)
5 Pointers PIC-17212

Declaring pointers:
Pointers are also variables and defined in a program in same manner like any other variables. The
rules for using pointer variable name are same as ordinary variables. The data type of pointer is same
as the data type of variable to which it is pointing e.g. if address of integer variable is to be stored in
a pointer variable then the type of pointer variable will be int.
The syntax for declaring pointer variable is as follows:
data_type *pointer_variable_name;
e.g. To declare integer pointer
int *ptr;
Similarly,
float *fptr;
double *dptr;
char *chptr;
& operator is used to access the address of a variable. e.g consider
int a=10;
&a will return address of variable a. To store the address of variable a into a pointer variable,
first of all a pointer variable is declared of int type e.g.
int *ptr;
Now to store address of variable a in pointer variable, following statement can be used:
ptr = &a;
Similarly to store address of floating point variable, a pointer variable of float type is used e.g.
float f;
float *fptr;
fptr = &f;

Accessing pointers:
Once a pointer variable is declared, address of an ordinary variable can be stored in it. To access
address of a variable,& operator can be used. The general form for storing address of a variable in
pointer variable is:
pointer_variable_name = &variable_name;
e.g.
int a=50;
int *p;

2
Computer Department, Jamia Polytechnic (0366)
5 Pointers PIC-17212

p=&a;

To print address of a variable %u format specifier is used. e.g.


printf(address of a=%u,&a);
or printf(address of a=%u,p);
In the above statement, p is pointer variable in which address of variable a is stored.
To access the value stored at a location pointed by a pointer variable, * operator can be used.
e.g.
int a=100;
int *ptr;
ptr=&a;
printf(value of a=%d,a);
printf(value of a=%d,*ptr);

Program #1: To print address and value of a variable:


#include<stdio.h>
#include<conio.h>
int main( )
{
int num;
int *ptr;
clrscr( );
printf(Enter a number: );
scanf(%d,&num);
ptr=&num;
printf(Address of num= %u,ptr);
printf(\nValue of num= %d,num);
printf(\nValue of num by using pointer= %d,*ptr);
getch( );
return 0;
}
Output of the program:
Enter a number: 23
Address of num= 65524
Value of num= 23
Value of num by using pointer= 23

3
Computer Department, Jamia Polytechnic (0366)
5 Pointers PIC-17212

Program #2: To find sum of two values using pointers:


#include<stdio.h>
#include<conio.h>
int main( )
{
int a, b, sum;
int *p1, *p2;
clrscr( );
printf(Enter any two numbers: );
scanf(%d%d,&a,&b);
p1=&a;
p2=&b;
sum=(*p1)+(*p2);
printf(\nSum= %d,sum);
getch( );
return 0;
}

Pointer arithmetic:
Following operations can be performed on a pointer:
1) Addition of a number to a pointer:
A value can be added to pointer variable e.g.
int i = 4, *p;
p = &i;
p++;
p--;
p = p+5;
p = p+3;
When a value 1 (one) is added to a pointer variable, the size of data type is added to it. e.g. if an
integer pointer is incremented by 1 then value 2 is added to it because size of integer data type is 2
bytes.
In general, when a value is added to pointer variable, it is multiplied by the size of data type and
result of multiplication is added to the pointer variable.

2) Subtraction of a number from a pointer:


A value can be subtracted from a pointer variable e.g. if pointer variable is p then:
p--;

4
Computer Department, Jamia Polytechnic (0366)
5 Pointers PIC-17212

p = p 3; etc. operations are possible.


Again, like addition, the value to be subtracted from pointer variable will be multiplied by the size of
data type and the result of multiplication will be subtracted from pointer.

3) One pointer variable can be subtracted from other:


One pointer variable can be subtracted from other provided that both variables point to elements of
the same array. e.g.
#include<stdio.h>
#include<conio.h>
int main( )
{
int arr[ ]={10, 20, 30, 45, 67, 53, 79};
int *i, *j;
clrscr();
i=&arr[1];
j=&arr[5];
printf(%d, j-i );
printf(\n%d, *j - *i);
getch();
return 0;
}
Output of the program is:
4
33
The output on first line is 4 because there is a difference of four elements of array. The second line of
the output has 33 because 53-20=33.

4) Two pointer variables can be compared:


If there are two pointer variables and if both pointers point to objects of the same data type then we
can compare these two pointers. e.g. two int pointers can be compared, two float pointers can be
compared and so on.
Consider following code fragment:
int a, b;
int *ptr1, *ptr2;
a = 10;
b = 20;
ptr1 = &a;
ptr2 = &b;

5
Computer Department, Jamia Polytechnic (0366)
5 Pointers PIC-17212

if(ptr1<ptr2)
{

}
Since ptr1 and ptr2 are of type int, the above comparison in if statement is valid.

Pointers and arrays:


An array is a collection of same type of elements. Array elements are always stored in contiguous
memory locations. If a pointer is assigned with the address value of first element of array, when
pointer is incremented by 1, it will point to next array element.
Suppose there is an array num = {13,33,23,47,59,17,99}. Following figure shows how this array is
located in memory:

We can declare a pointer of type int and assign address of first array element:
int *ptr;
ptr = &num[0];
The current value of ptr is 65524 as it is pointing to num[0]. When ptr is incremented by 1, it will
point to next element of the array because, after incrementing, 2 will be added to ptr since it is of
type int. Its value will become 65526 which is address of num[1].
Following example shows an array of floating point numbers. The array elements are accessed by
using pointer and the sum of all elements is calculated. Finally average of the elements is displayed.

#include<stdio.h>
#include<conio.h>
int main( )
{
float arr[ ]={1.2, 5.7, 2.51, 9.31, 7.689};
float sum=0, avg;
float *ptr;
int i;
clrscr();
6
Computer Department, Jamia Polytechnic (0366)
5 Pointers PIC-17212

ptr=&arr[0];
for(i=0;i<5;i++)
{
sum=sum+(*ptr);
ptr++;
}
printf(Sum of elements=%f, sum);
avg=sum/5.0;
printf(\nAverage of elements=%f, avg);
getch();
return 0;
}
Output of the program is:
Sum of elements=26.409000
Average of elements=5.281800

Program #3: To find smallest element of array using pointers:


#include<stdio.h>
#include<conio.h>
int main( )
{
int arr[ ]={111, 75, 87, 52, 45};
int *p;
int smallest;
int i;
clrscr();
p = &arr[0];
smallest = *p;
for(i=1;i<5;i++)
{
p++;
if(smallest > *p)
smallest = *p;
}
printf(\nSmallest of array elements = %d, smallest);
getch();
return 0;
}

Output of the program is:


Smallest of array elements = 45

7
Computer Department, Jamia Polytechnic (0366)
5 Pointers PIC-17212

Pointers and structures:


Pointers can be of struct type. Structures are used to create user-defined data type. In ordinary

structure variable, we can access members by dot ( . ) operator. If a pointer of struct type is defined,
to access the members of structure using pointer -> operator is used. The syntax for accessing a
member through pointer is:
pointer-name -> member
For example:
#include<stdio.h>
#include<conio.h>
struct employee
{
int emp_id;
char *emp_name;
float salary;
};

int main( )
{
struct employee emp1;
struct employee *ptr;
clrscr();
emp1.emp_id = 123;
emp1.emp_name = Stephen Leacock;
emp1.salary = 17875.25;
ptr = &emp1;
printf(Employee Id = %d\n, ptr -> emp_id);
printf(Employee Name = %s\n, ptr -> emp_name);
printf(Salary = %f, ptr -> salary);
getch();
return 0;
}
Output of the Program is:
Employee Id = 123
Employee Name = Stephen Leacock
Salary = 17875.250000

End of Chapter 5

8
Computer Department, Jamia Polytechnic (0366)

You might also like