-1

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.

John Crash
  • 21
  • 5
  • 2
    The code you have posted causes undefined behaviour (although it is somewhat common in real code! relying on compilers not performing optimizations that they are allowed to perform). It'd improve your question if you could show a full working example showing what you mean. The `strstr` etc. functions do not use this technique – M.M Oct 30 '15 at 03:30
  • 2
    Multiple misconceptions...(1) You don't necessarily need to use `fnc(&str)` — using `fnc(str)` would be sufficient for many purposes; (2) once inside the function, it is not necessarily the case that you'd use `*(char **)ptr;` inside the function; (3) `strstr()` and `strcmp()` do not take `void *` arguments. What you describe might be correct if the called function needed to modify the pointer in the calling function, but that requires passing a pointer to pointer as a `void *`, which is not really what you suggested. – Jonathan Leffler Oct 30 '15 at 04:08
  • 1
    Inside the parentheses: `char**` is just a type. By placing the type in parentheses you have created a *typecast*. It's a pointer to a pointer to a `char`. Outside the parentheses is a *dereference*; that is, you're accessing the `char*` pointer that the `char**` points to. – mpontillo Oct 30 '15 at 04:11
  • 3
    Possible duplicate of [Concept of void pointer in C programming](http://stackoverflow.com/questions/692564/concept-of-void-pointer-in-c-programming) – Ken White Oct 30 '15 at 04:17

1 Answers1

1

Maybe you should read a introductory C book that explains the concept of pointers. You seem to be a little bit hazy on that.

Then you must thing of void * as a pointer that can point to any memory location but has no type information of what that location contains.

When you want to use that location you have to tell the compiler to what type the void pointer is pointing to by casting to a pointer of appropriate type.

RedX
  • 14,749
  • 1
  • 53
  • 76
  • You along with some comments (Mike and Jonathan Leffler) above helped me. Thanks so much for that – John Crash Oct 30 '15 at 04:39
  • 1
    You will never get an answer to that RedX. People downvote when the written does not fit their world. I upvoted, you cared to answer and your answer is ok and it fits the OP's level of understanding. – Ely Oct 30 '15 at 05:27