0

I am not familiar with the syntax below for the following struct

struct fp {
  int (*fp)();
}

What is int (*fp)() ? I understand that it is an integer and *fp is a pointer but I do not understand what that parentheses in (*fp)() do.

Liondancer
  • 15,721
  • 51
  • 149
  • 255

4 Answers4

4

fp is a pointer to a function with empty parameters list.

I.e.

int myfunc()    //define function
{
    return 0;
}

struct fp    //define structure
{
  int (*fp)();
} mystruct;
mystruct.fp = &myfunc;    //assign function pointer to structure element

int a = mystruct.fp();    //Call function through pointer

There are many ways to read C declarations, that can be very complicate in some cases. Start reading this https://parrt.cs.usfca.edu/doc/how-to-read-C-declarations.html.

You can google for "how to read c declarations" for more in depth explanation and further tips.

As Swordfish pointed out, the use of empty parameter list imply other sensible points about function definitions, that could be worth to deep. Please refer to Swordfish's comment below for the main points relative to functions definition.

I will refer only to the §6.11.6 Function declarators (of §6.11 Future language directions):

The use of function declarators with empty parentheses (not prototype-format parameter type declarators) is an obsolescent feature.

Frankie_C
  • 4,764
  • 1
  • 13
  • 30
  • *empty parameters list* – might be misleading. – Swordfish Feb 06 '19 at 08:30
  • 1
    @Swordfish I agree, but the problem is that the empty **parameter list** (not just no parameters) is another complication of the language. While it comes from old ANSI standard (and still mantained for compatibility reasons), could have at least 2 different meanings: no parameters at all, or variable number (variadic function). So I would add no more complication to the answer. Or do you have different suggestion? – Frankie_C Feb 06 '19 at 08:45
  • Maybe at least mention that empty parameter lists are depreciated for a long time now and shall no longer be used in new code. With the function pointer type from the example one can call every function returning an `int` as long as the correct number and type of parameters are provided on call. But then the next thing is default parameter promotions :/ – Swordfish Feb 06 '19 at 08:54
  • @Swordfish You said it! Just going a little bit in detail we are drowning in: programming style, or bad programming habits, function definitions, ANSI compliance, future language directions, etc. Anyway I think that your comment is already a contribution, and refer to it in the answer. – Frankie_C Feb 06 '19 at 10:11
2

It is a function pointer. They are powerful and quite hard to get your head around if you are a beginner.

Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
1

It is a declaration of the variable fp which is a pointer to a function that returns an int and takes an unspecified list of arguments.

Leo
  • 741
  • 6
  • 14
1

It is a strcture that wraps a function-pointer.

Have a look here: How do function pointers in C work?

chris01
  • 10,921
  • 9
  • 54
  • 93