Function pointer, help please!

Hi,
I am learning about pointer, right now with pointer function. Lets assume, a pointer to function which takes no arguments and returns a double. So it would be,

double(*myFunc)();

Now I want a pointer to function which takes no arguments and returns ten pointers to functions, which takes no arguments and returns a double.

So wouldn't it be this:

double((*(*myFunc)())[10])();
?

But a book I am reading says quite different, it says this rather:

double (*(*(*myFunc)())[10])();

Why is this? Why I should put an extra asterisk there?
1
2
3
   double (*(*(*myFunc)())[10])();
              ^
              Pointer to function...


1
2
3
   double (*(*(*myFunc)())[10])();
            ^            ^
         ...that returns a pointer to an array of 10...


1
2
3
   double (*(*(*myFunc)())[10])();
   ^                          ^
...pointers to functions with return type double.


It easily gets very complicated using pointers to arrays and functions. I recommend you take a look at std::function (as an alternative to function pointers) and std::array and std::vector (as an alternative to arrays). It makes life much easier.

http://www.cplusplus.com/reference/functional/function/
http://www.cplusplus.com/reference/array/array/
http://www.cplusplus.com/reference/vector/vector/

This is what the equivalent code would look like using std::array and std::function:
 
std::function<std::array<std::function<double()>, 10>()> myFunc;
Last edited on
+1.

Also, whenever you must deal with nested types, it is worth your time to use typedefs.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>

// Disentangled typedef
typedef double          (*        double_fn )();
typedef double_fn             ten_double_fns [10];
typedef ten_double_fns* (*ptr_ten_double_fns)();

// In the book
typedef double (*(*(*myFunc)())[10])();


// Our ten functions
double a1()  { return 1; }
double a2()  { return 2; }
double a3()  { return 3; }
double a4()  { return 4; }
double a5()  { return 5; }
double a6()  { return 6; }
double a7()  { return 7; }
double a8()  { return 8; }
double a9()  { return 9; }
double a10() { return 10; }

// An array of those ten functions
ten_double_fns  array_of_a = { a1, a2, a3, a4, a5, a6, a7, a8, a9, a10 };
                
// A function that returns a pointer to that array
ten_double_fns* f() { return &(array_of_a); }


int main()
{
  // Dientangled typedef
  ptr_ten_double_fns f1 = f;
  
  for (int n = 0; n < 10; n++)
    std::cout << (*f1()) [n] () << " ";
  std::cout << "\n";
  
  
  // In the book
  myFunc f2 = f;
  
  for (int n = 0; n < 10; n++)
    std::cout << (*f2()) [n] () << " ";
  std::cout << "\n";
}

Remember that a function cannot return a dimensioned array -- so it returns a pointer to that array.

By the way, there's a site for function pointers:
http://www.newty.de/fpt/fpt.html

Hope this helps.
Topic archived. No new replies allowed.