0

How do I forward declare a function that has a function as a parameter in C?

Here is the function:

void start_thread (void* function (void*)) {

    ...

}

I tried forward declaring it as:

void start_thread (void*);

as well as:

void start_thread (void* (void*));

and:

void start_thread (void*, void*);

None of these were accepted. What is the correct day to forward declare for this function?

Edit:

void start_thread (void (*) (void*));

Yields:

Compilation error
prog.c: At top level:
prog.c:135:6: error: conflicting types for 'start_thread'
 void start_thread (void* function (void*)) {
      ^
prog.c:25:6: note: previous declaration of 'start_thread' was here
 void start_thread (void (*) (void *));
      ^
Bo Persson
  • 90,663
  • 31
  • 146
  • 203
Clinton J
  • 1,975
  • 3
  • 19
  • 31
  • 3
    `void (*)(void *)`. The rest can be found in a C book of your choice. – too honest for this site Sep 20 '16 at 15:19
  • What Olaf said. If you want to supply a name for tha function (for documentation purposes): `void (*functionName)(void *);` You were almost there chodobaggins, only missing one set of parenthesis. – Klas Lindbäck Sep 20 '16 at 15:24
  • typedef is your friend, and this is my favorite answer to a similar question http://stackoverflow.com/questions/1591361/understanding-typedefs-for-function-pointers-in-c – user3528438 Sep 20 '16 at 19:36

3 Answers3

0

Use the windscreen wiper technique (left-right-left...):

It's a pointer, so there is an *. On a function, so (*).

It return void, so, void (*).

It take a void*, so void (*)(void*)

The prototype is void start_thread(void (*)(void*));

Pierre
  • 1,162
  • 12
  • 29
  • Compilation error prog.c: At top level: prog.c:135:6: error: conflicting types for 'start_thread' void start_thread (void* function (void*)) { ^ prog.c:25:6: note: previous declaration of 'start_thread' was here void start_thread (void (*) (void *)); – Clinton J Sep 20 '16 at 17:15
  • There is an extra `*` (or you missed an `*`) in the return type – Pierre Sep 21 '16 at 14:59
0

For the function definition you have:

void start_thread (void* function (void*)) {

The argument is a function which accepts a void * as a parameter and returns a void *. To be more clear about the definition, it should be:

void start_thread (void *(*function)(void*)) {

So the declaration should be:

void start_thread (void *(*)(void*));
dbush
  • 205,898
  • 23
  • 218
  • 273
-2

The correct forward declaration/prototype would be:

void start_thread(void*(void*);
Clinton J
  • 1,975
  • 3
  • 19
  • 31
  • It looks like you forgot to correct an earlier or later declaration in the code you are attempting to compile after you received the correct prototype from the comment and the two answers. Please See: [**How to create a Minimal, Complete, and Verifiable example**](http://stackoverflow.com/help/mcve)." – David C. Rankin Sep 20 '16 at 17:29
  • Actually not true, those answers were incorrect; the answer I posted is the one that compiles correctly. – Clinton J Sep 30 '16 at 04:09
  • Did you try it? Or are you just saying that and downvoting my answer based on your expectation?. My code compiled exactly as above and ran perfectly. – Clinton J Dec 21 '16 at 02:45
  • @ClintonJ Your parentheses are mismatched. Did _you_ try it? – Max Langhof Apr 05 '19 at 15:02