c - How to understand dereferences and generic pointer in order to see the output? -
why output: aa b ee f ii j
? did line: void (*pfunc)(void*);
create function pfunc
similar function print
? know void pointer generic pointer still, how did output come way is? arguments @ command line: aaa eee iii
void print (void *a) { char**p=(char**)a; printf("%s",(*p+1)); putchar(''); putchar(**p+1); putchar(''); } int main(int argc, char *argv[]) { int i; void(*pfunc)(void*); pfunc=print; for(i=1; i< argc;i++) pfunc(argv+i); return 0; }
did row:
void (*pfunc)(void*);
creates functionpfunc
similar function print?
yes. void (*pfunc)(void*);
declare pfunc
pointer function return type void
, expects argument of type void *
.
why output:
aa b ee f ii j
?
the snippet
for(i=1; i< argc;i++) pfunc(argv+i);
passes of strings aaa
, eee
, iii
function print
. there casting a
char **
.
first string aaa
, *p
pointer first char
, *p+1
second char.
statement
printf("%s",(*p+1));
will print string second character, i.e print aa
.
**p
char
, **p+1
increment ascii value of character , putchar(**p+1);
print character. ascii value of a
97
, 98
b
.
therefore, output aaa
aa b
. same goes other arguments.
Comments
Post a Comment