# DEPARTMENT OF TECHNICAL EDUCATION ANDHRA PRADESH

Name Designation Branch Institute Year/Semester Subject Subject Code Topic Duration Sub Topic Teaching Aids : Murali Krishna Chintala : Lecturer in CME : Computer Engineering : SUVR & SR GPW, Ethamukkala : III Semester : UNIX & C : CM – 304 : Basics of Pointers : 50 Min : Pointers to functions : PPT, Animations
CM304.74 1

Objective
On completion of this period, you would be able to know …

 Understand pointers to functions.  Understand the concept of dynamic memory
management.

CM304.74

2

Pointers to functions
 A pointer to a function contains the address
of the function in memory.

 A pointer to a function is simply a pointer
whose value is the address of the function name.

 Pointers are often passed to a function as
arguments.
CM304.74 3

Pointers to functions
Contd..

Syntax of pointer to a function is return-type (*ptr)();

 ptr is a pointer to a function which returns the
value of type return-type

CM304.74

4

Pointers to functions
Example: int (*ptr)(int);

 where ptr is a pointer to a function that receives
an integer parameter and returns a value of type integer.

 Parenthesis around *ptr is necessary, because
‘*’ has a lower precedence than parenthesis enclosing the function.
CM304.74 5

Pointers to functions
 We can make a function pointer to point to a
specific function by simply assigning the name of the function to the pointer.

Example: int fun(int); int (*ptr)(int); ptr=&fun;
CM304.74 6

Example
#include<stdio.h> float fun(float); main() { float x,value; scanf(%f”,&x); value=(*fun)(x); /* function is called through pointer */ printf(“%f”,value); }
CM304.74 7

Example
Contd..

float fun(float y) { return(y*y); }

 fun receives the value of x and returns it’s
square.

CM304.74

8

Dynamic memory management functions

 Memory is allocated for each variable that is
defined.  Once specific location has been referred for a variable, the location is fixed during the program irrespective of their usage.

CM304.74

9

Dynamic memory management functions
Example:

Contd..

 If an array is declared with size 100, 100 memory
locations are reserved for the array.

 If the program requires less than 100 locations, the
unused allocated storage is not released back until the program ends execution.

CM304.74

10

Dynamic memory management functions
Contd..

 If it requires more than 100, the size of the array
must be increased and the program must be recompiled.

 An alternative to this fixed allocation of memory
is the dynamic allocation of memory .

CM304.74

11

Dynamic memory management functions
Contd..

 Under dynamic allocation scheme, storage is
allocated to a program and released back to the computer while the program is running, rather than fixed at compile time.

CM304.74

12

Dynamic memory management functions
Uses of dynamic memory allocation

Contd..

 Optimizes the usage of storage space.  Used to allocate additional storage space or
to release the unwanted memory space at run time.

 Extremely useful when dealing with list of
structures, as it allows the list to expand as new records are added and contract as records are deleted.
CM304.74 13

Summary
In this class, we have learnt about..

 Pointer to a function contains the address of the
function in memory.

 Under dynamic allocation scheme, storage is
allocated to a program and released back to the computer while the program is running, rather than fixed at compile time.
CM304.74 14

Quiz
1. Pointer to the function contains the address of the function in memory. a) True b) False c) None

CM304.74

15

Quiz
1. Pointer to the function contains the address of the function in memory. a) True b) False c) None

CM304.74

16

Quiz
2.Dynamic memory allocation optimizes the usage of storage space. a) True b) False c) None
CM304.74 17

Quiz
2.Dynamic memory allocation optimizes the usage of storage space. a) True b) False c) None
CM304.74 18

Assignment
1) Explain the concept of dynamic memory allocation. 2) Write the uses of dynamic memory allocation?

CM304.74

19