Professional Documents
Culture Documents
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:
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;
3
Computer Department, Jamia Polytechnic (0366)
5 Pointers PIC-17212
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.
4
Computer Department, Jamia Polytechnic (0366)
5 Pointers PIC-17212
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.
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
7
Computer Department, Jamia Polytechnic (0366)
5 Pointers PIC-17212
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)