Anda di halaman 1dari 20

DEPARTMENT OF TECHNICAL EDUCATION

ANDHRA PRADESH
Name : Murali Krishna Chintala
Designation : Lecturer in CME
Branch : Computer Engineering
Institute : SUVR & SR GPW, Ethamukkala
Year/Semester : III Semester
Subject : UNIX & C
Subject Code : CM – 304
Topic : Basics of Pointers
Duration : 50 Min
Sub Topic : Pointers to functions
Teaching Aids : 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
Contd..
Example:

 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
Contd..
Uses of dynamic memory allocation
 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
Frequently Asked Questions

1) Explain the concept of pointers to functions


with examples.

CM304.74 20