Algorithms Data Structures GATE Computer Science Postal Study Material
Algorithms Data Structures GATE Computer Science Postal Study Material
Computer Science
&
Information Technology (CS)
Postal Correspondence
Examination Oriented Theory, Practice Set
Key concepts, Analysis & Summary
C O N T E N T: A L G O R I T H M & D ATA S T R U C T U R E
1. C POINTERS ……………………………………………………………… 03-14
CHAPTER-1
C POINTERS
INTRODUCTION:
Pointers are frequently used in C, as they offer number of benefit to the programmer. They include
Pointer can be used to return multiple values from a function via function argument.
The use of pointer array to character string result in saving of data storage space in
memory.
Pointer provides an efficient tool for manipulating dynamic data structure such as
structure linked list queue, stacks and trees.
They increase the execution speed and thus reduce the program execution time.
Understanding pointers:
intx = 80;
x var iable
80 value
5000 address
Representation of variable
Pointer variable is nothing but a variable that contain an address which is a location of another
variable in memory.
For example:
Pointer variable
& operator can be used with simple variable or an array element. The following are the illegal
use of address operator
int x [10]
It x is an array then expression such as & x [0] and & x (i+3) are valid and represent the address
int * P
The declaration causes the compiler to allocate memory location for the pointer variable. Since memory
location has not been assigned any value. These locations may contain some unknown value in them.
Therefore they point to some unknown location.
P ? ?
int p, q, * r ;
int x, * p, y;
x 10 ;
p &x
It is also possible to combine the declaration of data variable and declaration of pointer variable and
Initialization of pointer variable in one step.
int x, * p & x ;
We can also define the pointer variable with initial value of null or zero. We can also assign the
constant value to pointer variable.
Pointer are flexible we can make same pointer to point different data variable in different statement.
We can also use different pointer to point the same data variable.
int x, y, * ptr ;
ptr & x ;
* ptr 25; // This statement put the value of 25 at the location whose address is the value of
pointer variable; we know the value of pointer variable (ptr) is the address of x.
Declaration x y ptr
Chain of pointers:
p2 p1 Variable
address 2 Address1 Value
Pointer p2 contain the address of pointer p1 which points to the location that contain the desired
value
For example
int * * p2 ;
Multiplication:
y * ptr * ptr
Multiplication of * p1 and * p2
Division:
Z * p2 / * p1
p1 p2 is also allowed. If p1 and p2 both points to same array then p1 p2 gives the
However
p1 Will cause the pointer p1 to point to next value of its type. suppose p1 is integer. Pointer
with initial value 2800 then after operation p1 value of p1 will be 2802 not 2801
Size of int 2
char 1
float 4
size of po int er 2
p 1000
& p 2000
* p 10
Then
* & p 1000
* * & p 10
2 D: a i i * * a i j
In 1-D array to get value stored we need one asterisk (pointer notation)
Pointer to one data type can be used to point another data type. This is possible by type casting
P i int* pf
↑ ↑
Pointer to integer pointer to float no
Difference between * p 3 and * p 3 . Since precedence of [] is more than * p 3 declare p as an
When an array is passed to a function as an argument, only the address of first element of an array is
passed but not the actual value of array element.
For example:
int x 10 ;
sort x
int x;
x 20;
change & x
pr int f "%d \ n, x ;
* p * p 10;
Output 30
When function change ( ) is called only the address of x is passed to the pointer P not value.
Parameters are passed by value in c, i.e. values being passed are copied into the parameter of called
function at the time when function’s invoked.
2. int * p 10
3. int * p 10
4. int * p void
5. int p char * a
P is a function that accepts an argument which is a pointer to character & return an integer
quantity
6. int * p char * a
7. int * p char * a
9. int p char *a
P is a function that accepts an argument which is a pointer to a character array returns an
integer quantity.
10. int p char * a ;
P is a function that accepts an argument which is an array of pointer to characters returns an
integer quantity
11. int * p char a ;
P is a function that accepts an argument which is a character array returns a pointer to an
integer quantity
12. int * p char *a ;
P is a function that accepts an argument which is a pointer to character array return a pointer to
an integer-quantity
13. int * p char * a
14. int * p char *a ;
P is a function that accept an argument which is a pointer to a character array return an integer
quantity
15. int * p char *a
P is a ptr to fun! That accepts an argument which is an array of pointer to character return a
pointer to an integer quantity.
18. int * p 10 char a ;
19. int * * p 10 char a
20. int * * p 10 char * a
Questions
int z , y
* * ppz 1
z * * ppz
* py 2
y * py
x x3
return x y z
Main ( )
int c, * b, **a
c 4, b & c, a & b
pr int f "%d ", f c, b, a
Ans:19
Explanation
a b c
2000 1000 4
3000 2000 1000
x p4 ppz
4 1000 2000
6000 5000 4000
So z = 5
* py 2 return 7
So y = 7
As value of x = 4
So x = x+3 return 7
So output is 7+7+5 = 19
pq
* pz
int i 0, j 1;
int main
f &i, & j ;
pr int f : %d %d \ n ", i, j ;
return 0;
Ans:0,2
Explanation:
i j p q
0 1 3000 4000
3000 4000 1000 2000
After pq
p q
4000 4000