284 Programming and Data Structures
9.3 POINTER DECLARATION
Pointer variables can be declared as follows.
Example
in t *x;
floa t * f;
char *y;
1) In the first statement Xx ' is an in te g e r p o in ter and it tells to the compiler that it holds the
address of any integer variable. In the same way ' f ' is a f l o a t p o in te r which stores the
address of any float variable and ' y ' is a ch aracter p o in te r that stores the address of any
character variable.
2) The in d ire c tio n o perator (* ) is also called the deferen ce operator. When a pointer
is dereferenced, the value at that address stored by the pointer is retrieved.
3) Normal variable provides direct access to their own values whereas a pointer provides indirect
access to the values of the variable whose address it stores.
4) The in d ire c t io n o pe rator ( * ) is used in two distinct ways with pointers, declaration
and deference.
5) When a pointer is declared, the star indicates that it is a pointer, not a normal variable.
6) When the pointer is dereferenced, the in d ir e c t io n o p e rator indicates that the value at
that memory location stored in the pointer is to be accessed rather than the address itself.
7) Also note that the indirection operator ( * ) is the same operator that can be used as the
multiplication operator. The compiler knows which operator to call, based on the context.
8) The is the address operator and it represents the address of the variable. The %u is used
with p r in t f () function for printing the address of a variable. The address of any variable is
a whole number. The operator ' & • immediately preceding the variable returns the address of
the variable. In the given below example ' &' is immediately preceded by the variable ' num'
which provides the address of the variable.
Programs without the use of pointer are described.
9.1 Write a program to display the address of the variable.
# include <stdio.h>
# include <conio.h>
xnainO
{
int num;
c lrsc r ();
printf ("Enter a Number = " );
scanf ("%d",&:num);
printf ("Value o f num =%dn",num);
printf ("Address ofnum~%un",&:num);
getcheO;
}
OUTPUT:
Enter a Number = 20
Value of num = 20
Address of num=4066