// ==========================================
// * Samples
// * Pointers to functions
// * Version 2.0
// ------------------------------------------
// * Language : C
// * Content : Program file
// ------------------------------------------
// * Attachment
// - pointers-to-functions.c
// ==========================================
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn
[
pointers-to-functions.c 2K ]
// ==========================================
// * Samples
// * Pointers to functions
// * Language : C
// * Version 2.0
// ==========================================
// ============ C-code: Begin ============
// --------------------------------------------------
char f1(int); // Declaration
char f1(int dummy) {return 'a';} // Implementation
char (*pf1) (int); // Variable
char *f2(int); // Declaration
char *f2(int dummy) { return (char*)0;} // Implementation
char *(*pf2)(int); // Variable
char (*f3(double)) (int); // Declaration
char (*f3(double d_dummy)) (int i_dummy) { return (char (*) (int))0;} // Implementation
char (*(*pf3)(double)) (int); // Variable
// --------------------------------------------------
// ===================================================================
// f1 is a 'function' that returns 'char' and takes an 'int-argument'.
// pf1 is a 'pointer-to-function'; the function returns 'char' and takes an 'int-argument'.
// f2 is a 'function' that returns 'pointer-to-char' and takes an 'int-argument'.
// pf2 is a 'pointer-to-function'; the function returns 'pointer-to-char' and takes an 'int-argument'.
// f3 is a 'function-1' that returns 'pointer-to-function-2' and takes a 'double-argument';
// 'function-2' returns 'char' and takes 'int-argument'.
// pf3 is a 'pointer-to-function-1'; the function-1 returns 'pointer-to-function-2' and takes a 'double-argument';
// 'function-2' returns 'char' and takes 'int-argument'.
// ===================================================================
// ---------------------------------------
typedef char (*pfunc_t1) (int);
typedef char *(*pfunc_t2) (int);
typedef char (*(*pfunc_t3)(double)) (int);
// ---------------------------------------
int main ()
{
char ch;
char* pch;
char (*pf4) (int);
pfunc_t1 pfunc1 = f1;
pfunc_t2 pfunc2 = f2;
pfunc_t3 pfunc3 = f3;
pfunc_t1 pfunc4;
ch = pf1 (1100);
pch = pf2 (1200);
pf4 = pf3 (1300.1);
ch = pf4 (1400);
ch = pf3 (1500.1)(1600);
ch = pfunc1 (2100);
pch = pfunc2 (2200);
pfunc4 = pfunc3 (2300.1);
ch = pfunc4 (2400);
ch = pfunc3 (2500.1)(2600);
return 0;
}