1

Here is a code snippet.

class NetworkRequestChannel {
public:
  ...

  NetworkRequestChannel(const unsigned short _port_number,
    void * (*connection_handler) (int *));
  ...
private:
  ...
}

My question is about the argument void * (*connection_handler) (int *). I have a vague idea how to read that, and it's as a void pointer to an object named connection_handler that is being then cast to an int pointer. But I know that's almost certainly wrong, and I'm not sure how else to interpret it.

8
  • 5
    It's a function pointer, accepting a function that takes a single int* as an argument and returns a void* value (a generic memory address). The parameter itself is named connection_handler. Commented May 7, 2016 at 22:55
  • Note that class NetworkRequestChannel indicates that this is C++, not C. The answer is the same, but the code shown is not valid C. Commented May 7, 2016 at 22:59
  • That's something else that I have been confused about for a while. I'll admit, this is for a class, which is why I was trying not to put too many details and just get the one simple answer. However, in all of my professors' assignments, his files are C files with .C and .H extensions. However, HE uses classes even in his own predefined files, and the makefile we are given uses g++ -g. It's like some C/C++ hybrid that he's trying to do that I haven't seen up until this class. Commented May 7, 2016 at 23:01
  • 2
    In my experience, professors aren't really known to use the best practices. :P Commented May 7, 2016 at 23:02
  • 1
    If you do not understand a C declaration try using cdecl.org. Commented May 7, 2016 at 23:14

2 Answers 2

1

The argument

void * (*connection_handler) (int *)

Is a pointer to a function taking one argument of type int* and returning a void*

Sign up to request clarification or add additional context in comments.

Comments

0

It's the syntax for a function pointer. The parameter in question accepting a function that takes a single int* as an argument and returns a void* value (a generic memory address).

You can read more about it here.

(That's quite an old article, and I encourage people to comment or edit this answer to list ones that emphasize modern C++ practices. Disregard this if the article I linked is actually up to date with the best practices!)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.