-1

When I review a C program , I saw some code like this :

typedef int (*ibm_ldap_search_s)(LDAP *, char *, int , char *, char * [],int , LDAPMessage **);

What does this mean?

Neowizard
  • 2,981
  • 1
  • 21
  • 39
Jiawei
  • 1
  • 1
  • have a look at https://cdecl.org/ – hetepeperfan Aug 20 '18 at 08:52
  • Its a typedef for a function pointer. See [this](https://stackoverflow.com/questions/1591361/understanding-typedefs-for-function-pointers-in-c). – Osiris Aug 20 '18 at 08:52
  • this creates a type alias called `ibm_ldap_search_s`, aliased to "pointer to function returning `int` and accepting the specified parameters as arguments. Judging by the name, it's used as a callback or a dynamic lookup within a library. – WhozCraig Aug 20 '18 at 08:52

1 Answers1

0

The constructions like rettype (* name )( arguments... ) are used for function pointers.

int (* f1 )(void);

f1 is a function pointer which takes no arguments and returns int.

typedef int (*ibm_ldap_search_s)(LDAP *, char *, int , char *, char * [],int , LDAPMessage **);

ibm_ldap_search_s is a aliased type (ie. typedef). It aliases a function pointer which takes arguments: (pointer to LDAP, pointer to char, int value, pointer to char, pointer to pointer to char, int value and pointer to pointer to LDAPMessage) and returns an int. [] in function declarations is equal to *, i mean char *[] is the same as char **.
Example:

typedef int (*ibm_ldap_search_s)(LDAP *, char *, int , char *, char * [],int , LDAPMessage **);

int ibm_ldap_search(LDAP *ldap, char *str1, int value1, 
                    char *str2, char *pointer_to_strings[], 
                    int value2, LDAPMEssages **messages) { 
    return 0; 
}

int main() {
   ibm_ldap_search_s bar = ibm_ldap_search;
   int value = bar(NULL, NULL, 1, NULL, NULL, 2, NULL);
   printf("Function returned $d\n", value);
   return 0;
 }
KamilCuk
  • 120,984
  • 8
  • 59
  • 111