Passing functions via pointers

Nov 2, 2015 at 8:11pm
Hello.
I was wondering: how should I call a function stored in a separate file that of whom I know nothing but the header?

If I had a function for a queue that takes void* (and therefore I can put anything into it) and I wanted to call the destruction function of the data I'm queueing passing it as a parameter to the destruction function of the queue itself, how should I call it?

This is the header
 
  void queue_destruction(queue_t* queue, void (internal_data_destr)(void*))


When I want to call it, how do I do it? If I write
1
2
3
  queue_t* some_queue = create_queue();
  // create and store 100 stacks in queue
  void queue_destruction(some_queue,(*stack_destruction)(stack));

Valgrind says "invalid use of void expression" at queue_destruction line.
Nov 2, 2015 at 8:36pm
When calling a function, you don't indicate the return type or parameter types - the compiler already knows.
Nov 2, 2015 at 9:53pm
1
2
3
4
5
6
7
8
9
10
11
12
void internal_data_destr(void *)
{
   // destroy something
}

...
main()
{
    queue_t *some_queue = create_queue();
    // create and store 100 stacks in queue
    queue_destruction(some_queue, stack_destruction);
}




}
Nov 2, 2015 at 10:10pm
Nov 3, 2015 at 8:06pm
LB and dhayden are right, but, even so, I still get two errors.
I've read that void* receiving functions can take any kind of pointer you throw at them, but these errors seem not to agree with that.

pruebas_alumno.c: In function ‘dynamic_mtest’:
pruebas_alumno.c:91:5: error: passing argument 2 of ‘queue_destruction’ from incompatible pointer type [-Werror]
queue_destruction(some_queue, stack_destruction);
^
In file included from pruebas_alumno.c:1:0:
queue.h:31:6: note: expected ‘void (*)(void *)’ but argument is of type ‘void (*)(struct stack_t *)’
void queue_destruction(queue_t* some_queue, void (stack_destruction)(void*));
^


"stack_destruction" is a function in a separate file (stack.c) belonging to an external structure (a stack) that I am including using "#include "queue.h".
I've implemented "stack_destruction" as well, but I've been told that I must treat it as if I didn't knew nothing about it other than pre, post conditions and it's header (which means I can't change what it does, receives or gives back).

What should I do then?
Also, must I copy the header of "stack_destruction" within "queue.c" for it to work? Or just incluing stack.h is enough?
Nov 3, 2015 at 9:09pm
You are not trying to convert from some pointer to void pointer, you are trying to convert from one kind of function to an entirely different kind of function. Don't do that.
Nov 3, 2015 at 9:56pm
I get that I am doing it wrong, but I can't find how to do it right.
I want to pass the whole function as a parameter, not the result, and I don't know how to do that.
The responses above still leave me with the errors in my previous post.
Last edited on Nov 3, 2015 at 10:04pm
Nov 3, 2015 at 11:11pm
Why don't you read through the tutorial I linked for you? It will seriously explain everything.
Topic archived. No new replies allowed.