C language - Invoking functions based on index value passed as argument - array of pointers to functions

libertypointer

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!

Eric Postpischil

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);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

array of pointers as argument in c functions

From Dev

Array of Function-Pointers with different functions return value (in C)

From Dev

Char pointers vs Int pointers passed to functions

From Java

Are vectors passed to functions by value or by reference in C++

From Dev

Invoking nested functions in the Dart programming language

From Dev

Array of constant pointers to functions

From Dev

Pointers and functions in c

From Dev

pointers to functions in c++

From Dev

Functions and pointers in C

From Dev

pointers to functions in c++

From Dev

C functions pointers

From Dev

Functions and pointers in C

From Dev

Invoking a functions based on string name in java

From Dev

Should/Can smart pointers be passed by reference in functions

From Dev

pointers and structures being passed through functions

From Dev

array elements as argument in functions

From Dev

Dapper: String is passed on as Text when invoking postgres functions

From Dev

Insert value multiple times into array based on argument passed

From Dev

In julia functions : passed by reference or value?

From Dev

Pointers, functions and arrays in D Programming Language

From Dev

Pointers, functions and arrays in D Programming Language

From Dev

Can Haskell inline functions passed as an argument?

From Dev

How to compare 2 functions passed as argument

From Dev

Functions and function pointers in C++

From Dev

Trouble with pointers and functions in C++

From Dev

Invoking C# Delegate Method when delegate is passed as Generic Argument

From Dev

Pointers passed by reference in Delphi (import functions from DLL)

From Dev

Pointers passed by reference in Delphi (import functions from DLL)

From Dev

Storing SYSCALL functions in array of function pointers

Related Related

  1. 1

    array of pointers as argument in c functions

  2. 2

    Array of Function-Pointers with different functions return value (in C)

  3. 3

    Char pointers vs Int pointers passed to functions

  4. 4

    Are vectors passed to functions by value or by reference in C++

  5. 5

    Invoking nested functions in the Dart programming language

  6. 6

    Array of constant pointers to functions

  7. 7

    Pointers and functions in c

  8. 8

    pointers to functions in c++

  9. 9

    Functions and pointers in C

  10. 10

    pointers to functions in c++

  11. 11

    C functions pointers

  12. 12

    Functions and pointers in C

  13. 13

    Invoking a functions based on string name in java

  14. 14

    Should/Can smart pointers be passed by reference in functions

  15. 15

    pointers and structures being passed through functions

  16. 16

    array elements as argument in functions

  17. 17

    Dapper: String is passed on as Text when invoking postgres functions

  18. 18

    Insert value multiple times into array based on argument passed

  19. 19

    In julia functions : passed by reference or value?

  20. 20

    Pointers, functions and arrays in D Programming Language

  21. 21

    Pointers, functions and arrays in D Programming Language

  22. 22

    Can Haskell inline functions passed as an argument?

  23. 23

    How to compare 2 functions passed as argument

  24. 24

    Functions and function pointers in C++

  25. 25

    Trouble with pointers and functions in C++

  26. 26

    Invoking C# Delegate Method when delegate is passed as Generic Argument

  27. 27

    Pointers passed by reference in Delphi (import functions from DLL)

  28. 28

    Pointers passed by reference in Delphi (import functions from DLL)

  29. 29

    Storing SYSCALL functions in array of function pointers

HotTag

Archive