For example passing a char* str
to a function that takes a void *ptr
I realize that in order to pass this I need to pass as:
fnc(&str)
and once inside the fnc(void *ptr)
function I need to dereference to use it
fnc(void *ptr){
*(char **)ptr = other string
}
I learned this by trial and error but never understood really why this happens
What does the *
outside mean? How about the 2 *
inside (char __)?
Edit:
for example a callback function passed str1 as "&str" and str2 as "an address to an array whose first element is a char*"
lfind(&str, arr, ...., cmp);
which goes into
int cmp_str_ptr(const void *str1, const void *str2){
return strcmp(*(char **)str1, *(char **)str2);
}
What I'm really wondering though is what the * mean on each level of indirection. *(char *)str1 vs *(char **)str1 vs *(char)str1 if the last one is valid and even combinations with *(char *)&str1.
What does a * outside the parenthesis mean versus one inside. Regardless of the functions used I want to know what these mean.
Final Edit:
Thanks to Mike for helping me solve this, I now realize that the real problem was in typecast and not necessarily in pointers in general. I have read K&R's the c programming language but I didn't understand until now.
In summary what I was asking is why strcmp needed *(char **)str1 as a value and after Mike's answer I see that it is casted as a pointer to a char*, so it needs to be dereferenced so as to follow strcmp(char *s1, char *s2) reqs.
The reason why lfind passed the first argument as &str is still somewhat unknown to me besides creating similarity on the callback function (two dereferences to a char ** rather than one cast and one dereference). I assumed this from Jonathan's answer where he mentions it is not necessary to use a *(char **)ptr inside the function.
Thanks again for everyone's help. And sorry for the relatively dumb question.