简体   繁体   中英

What does this function definition mean?

This function definition is found here. :

static 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM