1

Thank you for taking the time to read this, I looked for answers before posting but I'm very new to the language. This exercise I'm trying to do is from the book "Effective C: An introduction to professional C programming".

This is my first go at learning the language, and the exercise from the 2nd chapter of the book is as follows:

Declare an array of three pointers to functions and invoke the appropriate function based on an index value passed in as an argument

I am not totally sure I understand what it's saying, but I have a piece of functioning code I think does the job. However, I'm not sure if I'm interpreting it correctly. Here's my code:

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

void f0(int x) {
  printf("I am f0 and in index location %d\n", x);
}

void f1(int x) {
  printf("I am f1 and in index location %d\n", x);
}

void f2(int x) {
  printf("I am f2 and in index location %d\n", x);
}

int main(void){

  void (*f0p)(int);
  f0p = &f0;

  void (*f1p)(int);
  f1p = &f1;

  void (*f2p)(int);
  f2p = &f2;

  void *array[3] = {f0p, f1p, f2p};

  for (int i = 0; i < 3; i++) {
    void (*program)(int);
    program = array[i];
    program(i);
  }
  
  return 0;
}

this works after compiling and returns the following:

I am f0 and in index location 0
I am f1 and in index location 1
I am f2 and in index location 2

However, am I completing the exercise correctly? I don't think I'm technically using the index as an argument and calling the function, but I'm a noob. Any validation or correction / education you provide would be extremely appreciated. I spent many hours on this today!

1
  • Looks reasonable to me. Could you do it without the local variables, and declare the array like this: void *array[3] = {&f0, &f1, &f2}; Commented Jan 30, 2021 at 2:04

2 Answers 2

3

Pointers should not be converted between pointers-to-functions and pointers-to-objects (including void) except in special situations.

The array is better declared as an array of pointers to functions:

void (*arrray[])(int) = { f0, f1, f2 };

and the functions can be called without an intermediate variable:

array[i](i);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I updated my code with your suggestion and it works great, thank you so much.
0

You can also typedef functions and use syntax similar to "normal" pointers and arrays.

typedef void functype(int);

void f0(int x) {
  printf("I am f0 and in index location %d\n", x);
}

void f1(int x) {
  printf("I am f1 and in index location %d\n", x);
}

void f2(int x) {
  printf("I am f2 and in index location %d\n", x);
}

int main(void)
{
    functype *array[] = {f0, f1, f2};

    functype *ptr = f0;

    for(int x = 0; x < sizeof(array) / sizeof(array[0]); x++)
    {
        array[x](x);
    }

    ptr(4);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.