The example you provide is customizable to ask qsort() sort an array of char (char *) pointers. This comparator to which you provide is provided to each "pair" of elements that the algorithm requires at an address . two char pointers. qsort() uses the address based on the root address that you give it, adding size-bytes to the "element". Since each "element" is a char *, the size of each element is, in fact, the size of the pointer.
I modified the comparator to demonstrate what is being compared and what addresses are being transmitted. You will see that they all increase from the base address of the array containing all char * s.
char *mystrings[] = { "This", "is", "a", "test", "of", "pointers", "to", "strings" }; int cstring_cmp(const void *a, const void *b) { const char **ia = (const char **)a; const char **ib = (const char **)b; printf("%p:%s - %p:%s\n", a, *ia, b, *ib); return -strcasecmp(*ia, *ib); } int main(int argc, char *argv[]) { printf("Base address of our pointer array: %p\n\n", mystrings); qsort(mystrings, sizeof(mystrings)/sizeof(mystrings[0]), sizeof(char*), cstring_cmp); for (size_t i=0; i<sizeof(mystrings)/sizeof(mystrings[0]);i++) printf("%s\n", mystrings[i]); return 0; }
outputs the following result:
Base address of our pointer array: 0x100006240 0x100006240:This - 0x100006260:of 0x100006260:of - 0x100006278:strings 0x100006240:This - 0x100006278:strings 0x100006248:is - 0x100006240:strings 0x100006278:This - 0x100006240:strings 0x100006250:a - 0x100006240:strings 0x100006270:to - 0x100006240:strings 0x100006258:test - 0x100006240:strings 0x100006260:of - 0x100006240:strings 0x100006268:pointers - 0x100006240:strings 0x100006260:of - 0x100006240:strings 0x100006240:test - 0x100006248:This 0x100006248:test - 0x100006250:to 0x100006240:This - 0x100006248:to 0x100006260:of - 0x100006268:pointers 0x100006268:of - 0x100006270:a 0x100006270:a - 0x100006278:is 0x100006268:of - 0x100006270:is to This test strings pointers of is a
source share