function_pointers.c
changeset 24 9cdad6c45b47
child 27 81a574d60c15
equal deleted inserted replaced
23:7acfc5eda7ed 24:9cdad6c45b47
       
     1 /**
       
     2  * test/demos/function_pointers.c
       
     3  * Copyright (C) 2008 Markus Broeker
       
     4  */
       
     5 
       
     6 #include <stdio.h>
       
     7 #include <stdlib.h>
       
     8 
       
     9 typedef struct T {
       
    10 	int a;
       
    11 	int b;
       
    12 } T;
       
    13 
       
    14 int plus(T t)
       
    15 {
       
    16 	return t.a+t.b;
       
    17 }
       
    18 
       
    19 int minus(T t)
       
    20 {
       
    21 	return t.a-t.b;
       
    22 }
       
    23 
       
    24 int func(T t, int (*ptrFunc)(T))
       
    25 {
       
    26 	return ptrFunc(t);
       
    27 }
       
    28 
       
    29 int main(int argc, char **argv)
       
    30 {
       
    31 	T t = {
       
    32 		.a = 20,
       
    33 		.b = 10
       
    34 	};
       
    35 
       
    36 	printf("Result: %d\n", func(t, &plus));
       
    37 	printf("Result: %d\n", func(t, &minus));
       
    38 
       
    39 	return EXIT_SUCCESS;
       
    40 }