Coupon Accepted Successfully!


Question 1

How to declare a pointer to a function?

Use something like this

int myfunc(); // The function.
int (*fp)();  // Pointer to that function.

fp = myfunc;  // Assigning the address of the function to the pointer.

(*fp)();      // Calling the function.
fp();         // Another way to call the function.

So then, what are function pointers, huh?

In simple words, a function pointer is a pointer variable which holds the address of a function and can be used to invoke that function indirectly. Function pointers are useful when you want to invoke seperate functions based on different scenarios. In that case, you just pass in the pointer to the function you want to be invoked. Function pointers are used extensively when writing code with call back scenarios.

For example, when writing code involving a XML parser, you pass in the pointers to your event handling functions and whenever there is a specific event, the XML parser calls your functions through their pointers. This function whose pointer is passed is generally called as the call back function.

Here is an implementation of a Generic linked list which uses function pointers really well...


typedef struct list {
    void *data;
    struct list *next;
} List;

struct check {
    int i;
    char c;
    double d;
} chk[] = { { 1, 'a', 1.1 },
          { 2, 'b', 2.2 },
          { 3, 'c', 3.3 } };

// See how the print() function takes in a pointer to a function!
void print(List *, void (*)(void *));

void insert(List **, void *, unsigned int);
void printstr(void *);
void printint(void *);
void printchar(void *);
void printcomp(void *);

List *list1, *list2, *list3, *list4;

int main(void)
    char c[]    = { 'a', 'b', 'c', 'd' };
    int i[]     = { 1, 2, 3, 4 };
    char *str[] = { "hello1", "hello2", "hello3", "hello4" };

    list1 = list2 = list3 = list4 = NULL;

    insert(&list1, &c[0], sizeof(char));
    insert(&list1, &c[1], sizeof(char));
    insert(&list1, &c[2], sizeof(char));
    insert(&list1, &c[3], sizeof(char));

    insert(&list2, &i[0], sizeof(int));
    insert(&list2, &i[1], sizeof(int));
    insert(&list2, &i[2], sizeof(int));
    insert(&list2, &i[3], sizeof(int));

    insert(&list3, str[0], strlen(str[0])+1);
    insert(&list3, str[1], strlen(str[0])+1);
    insert(&list3, str[2], strlen(str[0])+1);
    insert(&list3, str[3], strlen(str[0])+1);

    insert(&list4, &chk[0], sizeof chk[0]);
    insert(&list4, &chk[1], sizeof chk[1]);
    insert(&list4, &chk[2], sizeof chk[2]);

    printf("Printing characters:");
    print(list1, printchar);
    printf(" : done\n\n");

    printf("Printing integers:");
    print(list2, printint);
    printf(" : done\n\n");

    printf("Printing strings:");
    print(list3, printstr);
    printf(" : done\n\n");

    printf("Printing composite:");
    print(list4, printcomp);
    printf(" : done\n");

    return 0;

void insert(List **p, void *data, unsigned int n)
    List *temp;
    int i;

    /* Error check is ignored */
    temp = malloc(sizeof(List));
    temp->data = malloc(n);
    for (i = 0; i          *(char *)(temp->data + i) = *(char *)(data + i);
    temp->next = *p;
    *p = temp;

void print(List *p, void (*f)(void *))
    while (p) 
        p = p->next;

void printstr(void *str)
    printf(" \"%s\"", (char *)str);

void printint(void *n)
    printf(" %d", *(int *)n);

void printchar(void *c)
    printf(" %c", *(char *)c);

void printcomp(void *comp)
    struct check temp = *(struct check *)comp;
    printf(" '%d:%c:%f", temp.i, temp.c, temp.d);

Question 2

Does extern in a function declaration mean anything?

The extern in a function's declaration is sometimes used to indicate that the function's definition is in some other source file, but there is no difference between

extern int function_name();


int function_name();

Question 3

If I have the name of a function in the form of a string, how can I invoke that function?

Keep a table of names and their function pointers:

int myfunc1(), myfunc2();


  char *name; 
  int (*func_ptr)(); 
} func_table[] = {"myfunc1", myfunc1,
                  "myfunc2", myfunc2,};

Search the table for the name, and call via the associated function pointer.

Question 4

What does the error, invalid redeclaration of a function mean?

If there is no declaration in scope then it is assumed to be declared as returning an int and without any argument type information. This can lead to discrepancies if the function is later declared or defined. Such functions must be declared before they are called. Also check if there is another function in some header file with the same name.

Question 5

How can I pass the variable argument list passed to one function to another function.

Good question

Something like this wont work

  display("Hello", 4, 12, 13, 14, 44);

display(char *s,...)

show(char *t,...)
  va_list ptr;
  int a;
  a = va_arg(ptr, int);
  printf("%f", a);

This is the right way of doing it

  display("Hello", 4, 12, 13, 14, 44);

display(char *s,...)
  va_list ptr;
  va_start(ptr, s);

show(char *t, va_list ptr1)
  int a, n, i;
  a=va_arg(ptr1, int);

  for(i=0; i   {
     n=va_arg(ptr1, int);
     printf("\n%d", n); 

Question 6

How do I pass a variable number of function pointers to a variable argument (va_arg) function?

Glad that you thought about doing something like this!

Here is some code


  int (*p1)();
  int (*p2)();
  int fun1(), fun2();

  p1 = fun1;
  p2 = fun2;
  display("Bye", p1, p2);

display(char *s,...)
  int (*pp1)(), (*pp2)();
  va_list ptr;
  typedef int (*f)(); //This typedef is  very important.


  pp1 = va_arg(ptr, f); // va_arg(ptr, int (*)()); would NOT have worked!
  pp2 = va_arg(ptr, f);





Question 7

Will C allow passing more or less arguments than required to a function.

It wont if the prototpe is around. It will ideally scream out with an error like

Too many arguments


Too few arguments

But if the prototype is not around, the behavior is undefined.

Try this out


int foo(int a);
int foo2(int a, int b);

int main(int a)
  int (*fp)(int a);

  a = foo();
  a = foo2(1);


int foo(int a)

int foo2(int a, int b)

Question 8

How can I return multiple values from a function?

You can pass pointers to locations which the function being called can populate, or have the function return a structure containing the desired values, or use global variables.

Question 9

Does C support function overloading?

Function overload is not present in C. In C, either use different names or pass a union of supported types (with additional identifier that give hints of the type to be used).

Most people think its supported because they might unknowingly be using a C++ compiler to compile their C code and C+= does have function overloading.

Question 10

What is the purpose of a function prototype?

A function prototype tells the compiler to expect a given function to be used in a given way. That is, it tells the compiler the nature of the parameters passed to the function (the quantity, type and order) and the nature of the value returned by the function.


Test Your Skills Now!
Take a Quiz now
Reviewer Name