Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

explain the pointers to pointers issue

Tags:

c

I'm new into C and I'm trying to get a grip on pointers. I came across this expression

char *foo = *(char **)bar;

what exactly is going on? I understand that * is pointer ** is pointer to pointer, but I don't really get what is *(char **)bar

edit: worth to mention that bar is declared as const void *bar

like image 375
webduvet Avatar asked Sep 24 '26 14:09

webduvet


1 Answers

*(char**)bar;

Cast bar to a pointer to pointer to char and dereference, which results in a pointer to char (i.e., a char*).

Remember that pointers are merely indirection. When you dereference a pointer you "drop a star", i.e.:

char ***p = ...;
p    -> char***
*p   -> char**
**p  -> char*
***p -> char
like image 78
Ed S. Avatar answered Sep 26 '26 10:09

Ed S.