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