0

Here is the code:

// Global Definitions/Declarations:

typedef void * LOGHANDLE;

typedef LOGHANDLE (STD_CALL *LogOpen_T)
       (unsigned char *, 
        unsigned char *, 
        unsigned long, 
        unsigned long *);

LogOpen_T LogOpen;

// Inside some function:
    ...
    LogOpen = (LogOpen_T)ImportSymbol(moduleHandle, "LogOpen" );
    if (LogOpen == NULL)
    {
         err = -2;
    }
    ...

I am not able to understand how the typedef is been used here. Please explain.

4
  • 1
    Remove the word "typedef" ... do you understand it then? If not, your problem isn't with typedef. if so, then you shouldn't have any trouble understanding the typedef.
    – Jim Balter
    Sep 11, 2013 at 6:28
  • @JimBalter: I still remember how irritated I was seeing a typedef for a function pointer for the first time.
    – alk
    Sep 11, 2013 at 6:29
  • @alk I can't relate. Like I said, if you already understand declarations of function pointers -- and that's the annoying part because of how C buries the name being declared inside a bunch of syntax -- then understanding a typedef is straightforward, as it just declares the name as a synonym for the type rather than being a variable of the type.
    – Jim Balter
    Sep 11, 2013 at 7:59

3 Answers 3

3

The typedef itself defines a type alias for a pointer to a function where the function seems like:

void * STD_CALL f(unsigned char *, unsigned char *, unsigned long, unsigned long *);

The variable LogOpen is the actual pointer-to-a-function. Later the result of ImportSymbol is cast to the pointer-to-a-function.

2

LOGHANDLE defines a pointer which can point to anything. LogOpen_T defines a function pointer.

1

this is a pointer to a function of prototype

LONGHANDLE functionname
   (unsigned char *, 
    unsigned char *, 
    unsigned long, 
    unsigned long *);

example of use

LOGHANDLE mylogfunction
   (unsigned char *, 
    unsigned char *, 
    unsigned long, 
    unsigned long *){/* code*/}


LogOpen_T function_handle = (LogOpen_T)(&mylogfunction);

Not the answer you're looking for? Browse other questions tagged or ask your own question.