Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
132 views
in Technique[技术] by (71.8m points)

c++ - Passing Function Pointer

I'm a bit confused as to how I would pass a pointer to a pointer function. I have a function that takes a pointer to a function which I understand without problem (ExecBlock). But I'm given another prototype of a function (ExecBlock2) which takes the dereferenced-pointer (I'm not sure exactly what it is) and also takes the argument if passed function has any. If someone could explain the precedence and exactly what dereferencing a pointer function would do. Isn't that just passing the function itself?. What does (void *) do in this case?

int ExecBlock (void (*f) (void), int isSet)
{
    return ExecBlock2( HOW TO PASS HERE? , NULL, isSet);
}

int ExecBlock2(void *(*f)(void *), void *arg, int isSet)
{
    ... more code
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
void (*f) (void)

means pointer to function with no arguments returning void.

void *(*f)(void *)

means pointer to function taking a void pointer and returning a void pointer.

Since the types are different, the compiler will not allow you to pass one to the other without casting. (Note that casting is not really the right answer here, and as @detly points out, results in undefined behavior.)

As for dereferencing pointers to functions, you don't have to explicitly put a "*" before a function pointer to call it. For instance, you could call your function pointer f just by doing

f();

A function pointer example

Say you have a function f, which you'd like to pass to a function called takes_a_function. takes_a_function will probably have a type like

void takes_a_function(void (*f)(void *data), void *data);

Notice how there are two arguments to takes_a_function, a function pointer, and a void pointer to some data. Also note that the function f happens to take a void pointer as an argument. The idea is that you can pass the data to takes_a_function, and it will pass it along to f. For example, takes_a_function could be defined like

void takes_a_function(void (*f)(void *), void *data) {
  f(data);
}

Now, let's write a function to pass to takes_a_function. Our function will just print an int that is passed to it.

void prints_an_int(void *data) {
  // The idiom for converting a void pointer to another kind
  // of pointer.  NO NEED TO CAST.  Note this behavior is only
  // defined if the pointer data really does point to an int.
  int *i = data;
  printf("%d", *i);
}

int i = 0;
takes_a_function(prints_an_int, &i);

A couple of key points about this example:

  • prints_an_int has the same type as the function pointer expected by takes_a_function. No need to cast.
  • There's no need to use the & operator to create a reference to a function. This is why we can pass prints_an_int to takes_a_function directly. But we could also say takes_a_function(&prints_an_int, &i), and it would be the same.
  • void* basically means "pointer to unknown type." To actually do anything with it, you must assign a variable of type void* to another pointer variable whose type you expect. This is only guaranteed to work if you actually pass in the correct pointer type! In this example, we can assign data to an int*, since data really does point to an int. If you want more data than just an integer, a common pattern is to create your own struct type which includes all the fields you want, and pass that instead.
  • As a special case, the compiler does not require you to cast when assigning void pointers to other pointers and vice-versa. But again, you only get defined behavior if you eventually convert a void pointer back to the correct type.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...