Chapter:C function pointers Exercises

From Juneday education
Jump to: navigation, search

Create a small math library

We'll begin with writing some super duper awesome functions that calculate the min and max value of two integers. Here's the prototyupes of the functions:

int min(int a, int b);
int max(int a, int b);

Expand using link to the right to see a possible solution/answer.

math-lib.h

#ifndef MATH_LIB_H
#define MATH_LIB_H
int min(int a, int b);
int max(int a, int b);
#endif /* MATH_LIB_H */

math-lib.c

#include "math-lib.h"

int min(int a, int b)
{
  if (a>b)
    {
      return a;
    }
  return b;
}

int max(int a, int b)
{
  if (a<b)
    {
      return a;
    }
  return b;
}

Use the small math library

Add a c file with a main function that uses the two functions above. We'll start of by using the functions the plain old normal way, i e not using function pointers.

Expand using link to the right to see a possible solution/answer.

math-lib.h

#ifndef MATH_LIB_H
#define MATH_LIB_H
int min(int a, int b);
int max(int a, int b);
#endif /* MATH_LIB_H */

math-main.c

#include <stdio.h>

#include "math-lib.h"


void print_min(int a, int b)
{
  printf("min of (%d,%d) is: %d\n",
         a, b, min(a,b));
}

void print_max(int a, int b)
{
  printf("max of (%d,%d) is: %d\n",
         a, b, max(a,b));
}

int main(void)
{
  print_min(12,3);
  print_max(12,3);

  return 0;
}

Create two function pointer

Create two function pointers for the function min and max as defined above.

Expand using link to the right to see a possible solution/answer.

math-main-fp.c

int (*min_fun)(int a, int b);
int (*max_fun)(int a, int b);

Assign the two function pointer

Assign a value (the corresponding functions) to the two function pointers.

Expand using link to the right to see a possible solution/answer.

math-main-fp.c

  min_fun = min;
  max_fun = max;

Typedef the two function pointer

Write code that creates two types of function pointers, pointing to a max and a min function.

Expand using link to the right to see a possible solution/answer.

math-main-fp-td.c

typedef int (*min_fun_ptr)(int a, int b);
typedef int (*max_fun_ptr)(int a, int b);

Use the typedef pointers

Write code that creates two variables of the newly created type, assigns a value (typically the corresponding functions) and invokes the two functions (via the typedefed function pointer variables).

Create two variables and assign them

  min_fun_ptr min_fun = min;
  max_fun_ptr max_fun = max;

Invoke the functions

  printf("min of (%d,%d) is: %d\n",
         3, 12, min_fun(3,12));

  printf("max of (%d,%d) is: %d\n",
         3, 12, max_fun(3,12));

Complete listing of math-main-fp.c

#include <stdio.h>

#include "math-lib.h"

typedef int (*min_fun_ptr)(int a, int b);
typedef int (*max_fun_ptr)(int a, int b);

int main(void)
{
  min_fun_ptr min_fun = min;
  max_fun_ptr max_fun = max;

  printf("min of (%d,%d) is: %d\n",
         3, 12, min_fun(3,12));

  printf("max of (%d,%d) is: %d\n",
         3, 12, max_fun(3,12));

  return 0;
}