Pointers 313
{
c lrs c r ( );
*(int *)p t = 12;
printf ("np= %d”,p);;
%
II
1* pt points to d */
*(float *)pt = 5.4;
printf ( “nr=%f',d);
pt=&c; 1* pt points to c V
*(char* )pt='S';
printf ("nc= %c",c);
}
OUTPUT
P=12
R=5.4
O S
Explanation In the above example variable p, d and c are variable of type in t , f l o a t and char
respectively. Pointer p t is a pointer of type void. All these variables are declared before main ( ) . The
pointer is initialized with the address of integer variable p i.e. the pointer p points to variable x.
The statement *(int *) pt = 12 assigns the integer value 12 to pointer p t i.e to variable p. The contents
of variable pis displayed using the succeeding statement. The declaration * (in t * ) tells the compiler
that the value assigned is of integer type. Thus, assignment of float and char type are carried out. The
statements *(int *) pt = 12, *(float *) pt = 5.4 and *(char* )pt='S' helps the compiler to exactly determine
the size of data type.
SU M M ARY
This chapter describes the most important feature of the C language i.e. pointer. The reader is brought
to light about the declaration and initialization of pointers. You have studied how to access variables
using their pointers. After having gone through the topic you are familiar with the effect of unary
operators on pointers of different data types as well as arithmetic operations with pointers. You are
also familiar with an array of pointers and the relation of pointer with arrays of different dimensions.
The reader is also made aware about how to make a chain of pointers i.e. how one-pointer points to
another pointer. Finally, association of strings with pointer is explained.
EXERC ISES
A] A nsw er the following questions.
1) What are pointers? Why are they important?
2) Explain the features of pointers.
3) Explain pointer of any data type that requires four bytes.
4) Explain the use of (* ) indirection operator.
5) Explain the effect o f++ and - operator with pointer of all data types.
6) What is an array of pointer? How is it declared?
7) Explain the relation between an array and a pointer.
8) Why addition of two pointers is impossible?
9) Which are the possible arithmetic operations with pointers?