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
Pointers 285
Explanation The physical memory location of a variable is system dependent hence, address of the
variable can't be predicted immediately. In the above example the address of the variable % num' i s
4066.
Here, in the figure given below three blocks are shown related to the above program.
The first block contains the variable name.
The second block represents the value of the variable.
The third block is the address of the variable
'num' where 20. is stored. Here, 4066 is the memory
address.
9.2 Write a program to display the addresses of different variables together with their values.
# include <stdio.h>
# include <conio.h>
void mainO
{
char c;
int i;
float f;
clrscrO ;
printf ("Enver alphabet number, flo a ts");
scanf ("% c %d %f',&LC,&i,&f);
printf ("Value ofc=% c i=% d f=%fn",c,i,f);
printf ("nAddress of(c)% c =%u",c,&cc);
printf ("nAddress of(i)% d =%u",i,&ci);
printf CnAddress offf)%.2f=%u",f,&cf);
getcheO;
1
OUTPUT:
Enter alphabet,number, float = C 20 2.5
Address of(c)c = 4061
Address of(i)20 = 4062
Address of(f)2.5 =4064
286 Programming and Data Structures
Explanation The program mentioned above declares three variables of different data types. Their
values are displayed together with respective addresses. Memory addresses are unsigned integers.
From the above example the programmer can understand that the difference between the two successive
addresses is not constant due to different data types.
Below given programs are based on the pointers.
9.3 Write a program to show pointer of any data type that occupies same space.
# include <stdio.h>
# include <conio.h>
void main ()
{
clrscr ();
printf (" t char %dbyte&c its pointer %d bytesn",sizeof(char),sizeof(char*));
printf (" t int %d byte & its pointer % d bytesn",sizeof(int),sizeof(int*));
printf (" tflo a t %d byte & its pointer %d bytesn",sizeof(float)fsizeof(float*));
QUTF.inC?
char 1 byte & its pointer 4 bytes
int 2 byte & its pointer 4 hytes
float 4 byte & its pointer 4 bytes
Explana tion In the above program using s i z eo f () operator size of the data type and its pointers
are calculated and displayed. It can be observed from the above program that the data type requires
different number of bytes for storage where as pointer of any data type requires four bytes.
9.4 Write a program to display the value of variable and its location-using pointer.
# include <stdio.h>
# include <conio.h>
void main ()
{
in t v=10/ *p;
clrscr ();
p=&cv;
prin tf("n Address o fv = % u",p);
printf (" n Value o f v = % d",*p);
printf (" n Address o fp = %u",&cp);
}
OUTPUT:
Address of v= 4060
Value of v = 10
Address of p = 4062
Pointers 287
Explanation In the above program 'v ' is an integer variable and its value is 10. The variable 'p ' is
declared as a pointer variable.
The statementp=&v assigns address of %v ' to 'p ' i.e. xp' is the pointer to variable 'v'. To access the
address and value of %v' pointer 'p ' can be used. The value of 'p ' is nothing but address of the
variable %v'.
To display the value stored at that location *p is used.
The pointer variables also have an address and are displayed using ' &' operator. The statement used
is printf ("n Address of p= %u", &p);
The above explanation is followed by the diagram below given .
Variable V
__
pointer p
406010
d
Address 4060 Address 4062
The value o f 1 v ' is 10 and it is stored at location 4060. Address of % v ' 4 0 6 0 is assigned to variable
% p 9. Address of pointer variable ' p ' is4062. Here, 'p ' is a pointer type variable, which points to the
variable ' v ' . Hence, it must be declared as in t v=10 and pointer variable as int *p ;
9.5 Write a program to print the value of variable by different ways. How would you use
and operators for accessing the value.
# include <stdio.h>
# include <canio.h>
void main ()
{in t a, *pa;
float b,*pb;
clrsc rO ;
printf ("Enter Integer &float Value
scanf ("% d %f',&a,&cb);
pa=&ca;
pb=&cb;
printf (" n Address ofa=%u",pa);
printf (" n Value ofa=%d",a );
printf ("n Value of a=%d",*(&ca));
printf ("n Value o f a=%d",*pa);
printf ("nAddress ofb=% u ",pb);
printf (" n Value of b = % 2 f',b );
printf ("n Value ofb=%.2f',*(&cb));
printf (" n Value o fb =% .2 f' *pb);
I
288 Programming and Data Structures
OUTPUT;
Enter Integer & float Value: 4 2.2Address of a= 4054Value of a= 4Value of a= 4
Value of a= 4
Address of b=4060
Value of b=2.20
Value of b=2.20
Value of b=*2.20
Explanation The output of the program is as shown above. The program illustrates for accessing
the values of variable ' a ' and 'b ' using pointers. The value of pointer 'p a ' is 4054 and the value it
points to is 4. The value of pointer 1 p b ' is 4 0 6 0 and it points to value 2.2.
Thus, from the above example w e have noticed the equivalencies as given below.
a=* (&a) =*pa
Explanation o f * (&a) Here, &a is used to get the address of the variable ' a '. The contents stored in
this location are accessed by The declaration * (&a) acts as *pa. A simple example is given to
understand the above concepts.
9.6 Write a program to print value of variable using different pointer notations.
# Include <stdio.h>
# include <conio.h>
void main ()
{
int vsslO/ *p;
clrscr () ;
p=&ty
printf (" n v = % d v = %d v = % d",v,*(txv),*p);
OUTPUT;
v = 10 v = 10 v = 10
Explanation The above program is to display the value of variable ' v ' . Three different syntaxes are
used in th ep rin tf () statement.
9.7 Write a program to print element and its address-using pointer.
# include <stdio.h>
# include <conio.h>
void main ()
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.202.27