typedef with function pointers


Hi

Am trying to understand usage of typedef with function pointers, if it at all possible that is?

I am getting the following error for the code below:

1
2
3
4
5
6
7
8
func_ptr3.cpp:10:16: error: expected unqualified-id before ‘)’ token
 typedef void (*)(individual_app *) void_f1_ptr;
                ^
func_ptr3.cpp:10:36: error: expected initializer before ‘void_f1_ptr’
 typedef void (*)(individual_app *) void_f1_ptr;
                                    ^
func_ptr3.cpp:13:1: error: ‘void_f1_ptr’ does not name a type
 void_f1_ptr f_ptr = f1;


can anybody suggest if such a typedef is possible? Appreciate that "auto" is more applicable, but I'd like to know if typedef can be used for such a scenario.

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
#include <iostream>
struct individual_app {
  char i_name[30];
  int i_rate[3];
};

void f1(individual_app *);

typedef void (*)(individual_app *) void_f1_ptr;

// f_ptr is a pointer to f1
void_f1_ptr f_ptr = f1;

int main() {
  individual_app  a = {"XXX" , {0,1,2}};
  individual_app  a1 = {"YYY" , {11,12,13}};
  individual_app  a2 = {"ZZZ", {21,22,23}};

  f1(&a);
  return 0;
}

void f1 (individual_app * a) {
  std::cout << "App name : " << a->i_name << std::endl;
}
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
#include <iostream>

struct individual_app {

  char i_name[30];
  int i_rate[3];
};

void f1(individual_app *);

// typedef void (*)(individual_app *) void_f1_ptr;
typedef void (*void_f1_ptr)( individual_app* ) ; // typedef

// simpler: in two steps
typedef void function_type( individual_app* ) ;
typedef function_type* void_f1_ptr ;

// type alias declaration:
using function_type = void ( individual_app* ) ;
using void_f1_ptr = function_type* ;

using void_f1_ptr = void (*)( individual_app* ) ;

// f_ptr is a pointer to f1
void_f1_ptr f_ptr = f1;

int main() {

  individual_app  a =  { "XXX" , {0,1,2} };

  f1(&a);
}

void f1 ( individual_app * a ) {

  std::cout << "App name : " << a->i_name << '\n' ; // std::endl;
}

http://coliru.stacked-crooked.com/a/e2516f73a64e8ecb
Topic archived. No new replies allowed.