Lecture%2013%20-%20%20Function%20Pointers

Lecture%2013%20-%20%20Function%20Pointers - Lecture 13...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Copyright @ 2009 Ananda Gunawardena Lecture 13 Function Pointers In this lecture Functions with variable number of arguments Introduction to function pointers Example of passing a function pointer to a function (qsort) Defining a function pointer Generic Data Types Functions with variable number of arguments Sometimes we may want to define a function that can take variable numbers of arguments. For example printf and scanf are two IO functions that take variable number of arguments. The prototype of printf is printf(char*, …); where first argument char* is a string that contains the format statement for the print string (eg: “%d %x %i “) and the 3 dots (…) indicate a variable number of arguments equal to number of format symbols in the print string. Let us consider writing our own function sum that can take a variable number of arguments. That is, we can call the function as sum(2,x,y) or sum(3,x,y,z) etc. #include <stdarg.h> /* header for variable argument list */ int sum(int argcnt, …) /* argcnt supplies count of other args */ { va_list ap; /* argument pointer */ int ans = 0; va_start(ap, argcnt); /* initialize ap */ while (argcnt-- > 0) /* process all args */ ans += va_arg(ap, int); va_end(ap) ; /* clean up before function returns */ return (ans); } The <stdarg.h> is a macro library that contains a set of macros, which allows portable functions, that, accepts variable argument lists to be coded. Functions that have variable argument lists (such as printf ()) do not use these macros are inherently non-portable, as different systems use different argument-passing conventions. There are few things in the above code that we need to understand.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Copyright @ 2009 Ananda Gunawardena va_start () This is a macro that is invoked to initialize ap, a pointer to argument list. This macro must be called to initialize the list before any calls to va_arg() . va_arg() This macro returns the next argument in the list pointed to by ap . Each invocation of va_arg() modifies ap so that the values of successive arguments are returned in turn. The type parameter is the type the argument is expected to be. This is the type name specified such that the type of a pointer to an object that has the specified type can be obtained simply by suffixing a * to type. Different types can be mixed, but it is up to the routine to know what type of arguments are expected. va_end() This macro is used to clean up; it invalidates ap for use (unless va_start() is invoked again). Introduction to Function Pointers Function Pointers provide an extremely interesting, efficient and elegant programming technique. You can use them to replace switch/if -statements, and to realize late-binding . Late binding refers to deciding the proper function during runtime instead of compile
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 11

Lecture%2013%20-%20%20Function%20Pointers - Lecture 13...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online