How are arguments passed to function pointers in C? -


in following code snippet reference, compare called main() without parameters being passed. assume taking ((char *)&key, (char *)string) 2 parameters needed function call. how work internally in c? compiler fill in arguments when compare called?

#include <search.h> #include <string.h> #include <stdio.h>  #define  cnt           2  int compare(const void *arg1,const void *arg2) {    return (strncmp(*(char **)arg1, *(char **)arg2, strlen(*(char **)arg1))); }  int main(void) {    char **result;    char *key = "path";    unsigned int num = cnt;    char *string[cnt] =  {       "path = d:\\david\\matthew\\heather\\ed\\simon","lib = path\\abc" };     /* following statement finds argument starts "path"         */     if ((result = (char **)lfind((char *)&key, (char *)string, &num,                   sizeof(char *), compare)) != null)       printf("%s found\n", *result);    else       printf("path not found \n");    return 0;   } 

how function pointers in c work?

compare called main() without parameters being passed.

no. referred main.

essentially, tells lfind "hey, if want compare entries, take function - can called way expect it."

lfind, then, knows , whenever needs compare 2 entries, puts arguments wherever put them on normal call (on stack, right registers or wherever, depending on calling conventions of architecture) , performs call given address. called indirect call , bit more expensive direct call.

let's switch simpler example:

#include <stdio.h>  int add1(int x) {     return x + 1; }  int times2(int x) {     return x * 2; }  int indirect42(int(*f)(int)) {     // call function passed 42 , return result.     return f(42); }  int indirect0(int(*f)(int)) {     // call function passed 0 , return result.     return f(0); }  int main() {     printf("%d\n", add1(33));     printf("%d\n", indirect42(add1));     printf("%d\n", indirect0(add1));      printf("%d\n", times2(33));     printf("%d\n", indirect42(times2));     printf("%d\n", indirect0(times2));     return 0; } 

here, call function add1() on own first, tell 2 other functions use function own purpose. same function.

and works - both functions called 33 me, 42 , 0. , results - 34, 43 , 1 first , 66, 84 , 0 2nd function - match expectations.

if have @ x86 assembler output, see

… indirect42: .lfb13:     .cfi_startproc     movl    4(%esp), %eax     movl    $42, 4(%esp)     jmp *%eax     .cfi_endproc 

the function gets given function pointer %eax, puts 42 expected , calls %eax. (resp., activated optimization, jumps there. logic remains same.)

the function doesn't know function called, how called.

indirect0: .lfb14:     .cfi_startproc     movl    4(%esp), %eax     movl    $0, 4(%esp)     jmp *%eax     .cfi_endproc 

the function same other one, passes 0 whatever function gets.

then, comes calling stuff, get

    movl    $33, (%esp)     call    add1     movl    %eax, 4(%esp)     movl    $.lc0, (%esp)     call    printf 

here have normal function call.

    movl    $add1, (%esp)     call    indirect42     movl    %eax, 4(%esp)     movl    $.lc0, (%esp)     call    printf 

here, address of add1 pushed stack , given indirect42 function can wants.

    movl    $add1, (%esp)     call    indirect0     movl    %eax, 4(%esp)     movl    $.lc0, (%esp)     call    printf 

the same indirect0.

(other stuff snipped works same)


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -