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?
314 Programming and Data Structures
10) Explain comparison of two pointers.
11) How one pointer points to another pointer?
12) How will you recognize pointer to pointer? What does the number of '*'s indicate?
13) How strings are stored in the pointer variables? Is it essential to declare length?
14) What is the base address? How is it accessed differently for one-dimensional and two-
dimensional arrays.
15) Distinguish between the address stored in the pointer and the value at that address?
16) Why does element counting of arrays always start from 'O'. ?
17) Write a program to read and display a two-dimensional array of 5 by 2 numbers. Reduce base
address of an array by one and start element counting from one.
B) A nsw er the following b y selecting the appropriate option.
1) Which of the following statement is true after execution of following program?
inta[5]={2,3}/c;
c=a;
(*c)-;
a) The value of a[0] will be 1; b) The value of a[0] will be 2;
c) The value of a[l]willbe2; d) None of the above.
2) The fastest way to exchange two rows in a two-dimensional array is
a) Exchange the addresses of each element in the two rows.
b) Exchange the elements of the two rows.
c) Store the addresses of the rows in an array of pointers and exchange the pointers.
d) None of the above
3) Which is the correct way to declare a pointer?
a) int *ptr;
c) int ptr*;
4) What will be the result of the following program?
void main ()
{
int a=8,b=2,c,*p;
c= (a=a+b,b=a/b,a=a*b,b=a-b) ;
p=&c;
p i r a p r M
printf ("n%d",++*p);
}
5) What will be the resulting string after the execution of the following program?
# include <stdio.h>
# include <conio.h>
# include <string.h>
mainO
{
char *s trl, *str2, *str3;
strl="The Capital of India is
str2="! ! ihleD weN" ;
str3="Bangalore";
stmcat (s tr l/strrev(str2) ,strlen(str3));
clrscr();
puts(strl);
I
b) * in t p tr;
d) in t_p tr x;
Pointers 315
a) The Capital of India is New Delhi b) The Capital of India is New Delhi!!
c) The Capital of India is Bangalore d) None of the above.
6) What will be the values of variables a and b after execution ?
# include <stdio.h>
# include <conio.h>
# include <string.h>
void main ()
{
int a, *b=&a, **c=&b;
8*5 /
**c=15;
*b= **c;
clrscr ()
printf ("A=% d, B=%d",a,*b);
}
a) A=15, B=15 b) A=15,B=5
c) A=15, B=16 d) None of the above.
7) What will be the value of variable a l and a2 after execution?
# include <stdio.h>
# include <conio.h>
mainO
{
int al,a2,c=3,*pt;
pt=&c;
al=3*(c+5);
a2=3*(*pt+5);
}
a) A=24, B=24 b) A=12, B=24
c) A=12,B=24 d) None of the above.
8) What will be the value of x after execution of the following program?
# include <stdio.h>
# include <conio.h>
void main ()
{
int x, *p;
p=&x;
*p=2;
clrscr()/
printf (" Value ofx=%d",x);
}
a) x=2 b) x=0
c) x=65504 d) none of the above
C] Attempt the following programs.
1) Write a program to accept string using charcter pointer and display it.
2) Write a program to calculate the square and cube of an entered number using pointer of a
variable containing the entered number.
3) Write a program to display all the elements of an array using a pointer.
..................Content has been hidden....................

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