function_pointers.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Thu, 16 Apr 2009 12:50:39 +0200
changeset 72 4103c76d5bf2
parent 27 81a574d60c15
child 77 49e0babccb23
permissions -rw-r--r--
sort.c: bubblesort fixed * the bubblesort algorithm has an abort-condition * the linearsort algorithm has not an abort-condition committer: Markus Bröker <mbroeker@largo.homelinux.org>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24
9cdad6c45b47 function pointer demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     1
/**
9cdad6c45b47 function pointer demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     2
 * test/demos/function_pointers.c
9cdad6c45b47 function pointer demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     3
 * Copyright (C) 2008 Markus Broeker
9cdad6c45b47 function pointer demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     4
 */
9cdad6c45b47 function pointer demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     5
9cdad6c45b47 function pointer demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     6
#include <stdio.h>
9cdad6c45b47 function pointer demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     7
#include <stdlib.h>
9cdad6c45b47 function pointer demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     8
9cdad6c45b47 function pointer demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     9
typedef struct T {
27
81a574d60c15 typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 24
diff changeset
    10
    int a;
81a574d60c15 typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 24
diff changeset
    11
    int b;
24
9cdad6c45b47 function pointer demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    12
} T;
9cdad6c45b47 function pointer demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    13
27
81a574d60c15 typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 24
diff changeset
    14
int plus (T t)
24
9cdad6c45b47 function pointer demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    15
{
27
81a574d60c15 typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 24
diff changeset
    16
    return t.a + t.b;
24
9cdad6c45b47 function pointer demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    17
}
9cdad6c45b47 function pointer demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    18
27
81a574d60c15 typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 24
diff changeset
    19
int minus (T t)
24
9cdad6c45b47 function pointer demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    20
{
27
81a574d60c15 typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 24
diff changeset
    21
    return t.a - t.b;
24
9cdad6c45b47 function pointer demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    22
}
9cdad6c45b47 function pointer demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    23
27
81a574d60c15 typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 24
diff changeset
    24
int func (T t, int (*ptrFunc) (T))
24
9cdad6c45b47 function pointer demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    25
{
27
81a574d60c15 typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 24
diff changeset
    26
    return ptrFunc (t);
24
9cdad6c45b47 function pointer demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    27
}
9cdad6c45b47 function pointer demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    28
27
81a574d60c15 typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 24
diff changeset
    29
int main (int argc, char **argv)
24
9cdad6c45b47 function pointer demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    30
{
27
81a574d60c15 typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 24
diff changeset
    31
    T t = {
81a574d60c15 typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 24
diff changeset
    32
        .a = 20,
81a574d60c15 typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 24
diff changeset
    33
        .b = 10
81a574d60c15 typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 24
diff changeset
    34
    };
24
9cdad6c45b47 function pointer demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    35
27
81a574d60c15 typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 24
diff changeset
    36
    printf ("Result: %d\n", func (t, &plus));
81a574d60c15 typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 24
diff changeset
    37
    printf ("Result: %d\n", func (t, &minus));
24
9cdad6c45b47 function pointer demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    38
27
81a574d60c15 typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 24
diff changeset
    39
    return EXIT_SUCCESS;
24
9cdad6c45b47 function pointer demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    40
}