:: Re: [DNG] Making sense of C pointer…
Top Page
Delete this message
Reply to this message
Author: Rainer Weikusat
Date:  
To: dng
Subject: Re: [DNG] Making sense of C pointer syntax.
karl@??? writes:

[...]

> To exemplify the "as they are used" statement, take a function pointer
> declaration:
>
> void (*log_func)(int priority, const char *format);
>
> here you cannot conveniently move the "*" to the "void" so it will look
> like a "pointer" declaration; it declares log_func to be something which
> if used as (*log_func)(a, b, d) will "give" you a void value.


Unless something more interesting is being done, function pointers don't
need to be dereferenced, they can be used just like functions. And
vice-versa. Eg, this useless little program converts all its arguments
to ints and prints a message stating if they were odd or even twice:

-------
#include <stdlib.h>
#include <stdio.h>

static void even(int d) 
{
    printf("%d is even\n", d);
}


static void odd(int d) 
{
    printf("%d is odd\n", d);
}


static void (*f[])(int) = {
    even,
    odd
};


int main(int argc, char **argv)
{
    int i;


    while (*++argv) {
    i = atoi(*argv);


    /* function name used as expression turns into function pointer */
    (i & 1 ? odd : even)(i);


    /* function pointer can be used like function */
    f[i & 1](i);
    }


    return 0;
}
------