开发者

What does this function definition mean?

This function definition is found here. :

s开发者_如何学运维tatic void (*resolve_memcpy (void)) (void)
{
    return my_memcpy; // we'll just always select this routine
}

I don't understand what it means.


resolve_memcpy is a function taking no arguments and returning a pointer to a function taking no arguments and returning void.

EDIT: Here's a link where you can read more about this kind of syntax: http://unixwiz.net/techtips/reading-cdecl.html


Here's my standard method for reading hairy declarations: start with the leftmost identifier and work your way out, remembering that absent any explicit grouping () and [] bind before *:

              resolve_memcpy               -- resolve_memcpy
              resolve_memcpy(void)         --  is a function taking no arguments
             *resolve_memcpy(void)         --  and returning a pointer
            (*resolve_memcpy(void)) (void) --   to a function taking no arguments
       void (*resolve_memcpy(void)) (void) --   and returning void
static void (*resolve_memcpy(void)) (void) -- and is not exported to the linker

So the return value of the resolve_memcpy function is a pointer to another function:

void (*fptr)(void) = resolve_memcpy();
fptr(); // or (*fptr)(), if you want to be explicit

If you want to drive your coworkers insane, you could write

resolve_memcpy()(); 

which will execute the function whose pointer is returned by resolve_memcpy.


One can use cdecl to find:

declare resolve_memcpy as static function (void) returning pointer to function (void) returning void


It basically returns a function pointer, which (presumably) you're supposed to use instead of memcpy.

// memcpy(...)
resolve_memcpy()(...) // Use this instead.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜