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.

0xF1
  • 6,046
  • 2
  • 27
  • 50
  • http://stackoverflow.com/questions/4295432/typedef-function-pointer – Sakthi Kumar Sep 11 '13 at 06:24
  • 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 '13 at 06:28
  • @JimBalter: I still remember how irritated I was seeing a `typedef` for a function pointer for the first time. – alk Sep 11 '13 at 06: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 '13 at 07:59

3 Answers3

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.

Juraj Blaho
  • 13,301
  • 7
  • 50
  • 96
2

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

alk
  • 69,737
  • 10
  • 105
  • 255
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);
dzada
  • 5,344
  • 5
  • 29
  • 37