Pointing to a function

Thursday, November 13, 2014
/* 1608.c: Pointing to a function */
#include <stdio.h>
/* function declaration */
int StrPrint(char *str);
/* main() function */
main()
{
    char str[24] = "Pointing to a function.";
    /* declaration of a pointer (ptr) to the StrPrint() function */
    int (*ptr)(char *str);
    /* StrPrint() function is assigned to the ptr pointer */
    ptr = StrPrint;
    /* calls the StrPrint() function via the dereferenced pointer ptr */
    if (!(*ptr)(str))
        printf("Done!\n");

    return 0;
}
/* function definition */
int StrPrint(char *str)
{
    printf("%s\n", str);
    return 0;
}

Copyright @ 2015 Tron!

Labels