The simplest, naive, explanation of char**
:
char c = 'a';
char* p1 = &a;
char** p2 = &p2;
p2
is a pointer to a pointer to a char
.
In an expression,
*p2
evaluates to a char*
.
**p2
evaluates to a char
.
However, there is more to it in the context of the function in your post. A "compare" function is used to sort objects. The standard library function qsort
needs a function with same signature as your compare
function to work correctly.
In your case, compare
returns a value that can be used by qsort
to sort an array of strings.
Given an array of strings such as:
char* strings[] = { ... }; // Initialize the strings
you can sort the strings by using
int numberOfStrings = ...;
qsort(strings, numberOfStrings, sizeof(*strings), compare);
qsort
calls compare
with the elements of strings
by using a void*
since qsort
is agnostic of the type of data being held by the first argument. The only portable way it can call the compare function is by passing void*
.
In the compare function, the user has to cast the pointers appropriately before making the comparisons.
When the compare function is expected to compare two strings, the void*
needs to be cast to char**
before calling strcmp
on the dereferenced pointer.