0

I have a C program in which this pointer to function is defined. And i am getting compilation error, what am i doing wrong?

typedef long (*myfunc)(long, long, long, long);

void mytest() {
  (*myfunc)(0, 0, 0, 0);
  myfunc(0, 0, 0, 0);
}

output from gcc ./test.c

$ gcc ./test.c 
./test.c: In function ‘mytest’:
./test.c:106:5: error: expected expression before ‘myfunc’
  106 |   (*myfunc)(0, 0, 0, 0);
      |     ^~~~~~
./test.c:107:10: error: expected identifier or ‘(’ before numeric constant
  107 |   myfunc(0, 0, 0, 0);
      |          ^

Edit:

compiler version

 $ gcc --version
gcc (GCC) 11.3.1 20220421 (Red Hat 11.3.1-3)

weima
  • 4,653
  • 6
  • 34
  • 55
  • 1
    `myfunc` is a type, therefore a variable of that type should be created, assigned and possibly called then. – alagner Jan 09 '23 at 07:10

1 Answers1

0

This is because myfunc is a type, and need to declare a variable first.

typedef long (*myfunc)(long, long, long, long);

void mytest() {
  myfunc abc = 0;
  abc(0, 0, 0, 0);
}

weima
  • 4,653
  • 6
  • 34
  • 55