Stack Exchange
How to qsort char**?
I want to sort an array of strings which was declared as following:
char** s;
s = calloc(4, sizeof(char *));
s[0] = "Banana";
s[1] = "Apple";
s[2] = "Grape";
s[3] = "Strawberry";

and for this I used qsort function:
size_t len = sizeof(s) / sizeof(char *);
qsort(s, len, sizeof(char *), cmp);

where cmp looks like this:
int cmp(const void* s1, const void* s2)
{
    const char** a = (const char**) s1;
    const char** b = (const char**) s2;
    return strcmp(*a, *b);
}

However, for this code, nothing is sorted and output is in the same order as it is in array. The problem is, I checked it with another way, where I declared array as (qsort and cmp stays untouched):
char* s[] = {"Banana", "Apple", "Grape", "Strawberry"};

and the output was perfectly sorted. I kind of understand it is because of, first method is double(pointer to pointer) pointer and the second one is pointer to array, but the work principles of them are the same. Any ideas how to sort char** s?. Thanks beforehand

You haven't allocated anything for each char* in s.  Whereas the bottom example is allocated on the stack.

@JoshAbraham In this very specific case, these are string literals and are allocated magically.

@EugeneSh. as parameters for cmp stand void* pointers, which is okay. However, in order to get strings, I am using this method. Moreover, I looked many resources, all of them were doing this same method

@MiradilZeynalli Right. Disregard my first comment (deleted it anyway..).

@EugeneSh. could you explain further?

@JoshAbraham Not in the comments. Search around about initialization of pointers with string literals, there are plenty of questions about it.

This code is correct code.char* s[] = {"Banana", "Apple", "Grape", "Strawberry"}; is correct and will behave as you expect it to be.
To make a clear statement - your use of sizeof is valid and it won't be a problem. Array won't decay here when used as an operand to sizeof.
char ** is indeed the right thing. Address of each element is being passed to cmp function.
The glitch is :-
In case char** s = calloc ... it would be wrong to use the sizeof because it is a pointer which contains the address of the allocated chunk. There is no way you can know the amount of memory allocated to it. So when you use the code with char**s = calloc .. specify the size of the array (not using sizeof) and it would work.
Is there any way to get len to be needed size for char** case?
Yes. You need to keep it separately in a variable. You can't apply sizeof and get the allocated memory size from a pointer variable.
How to make both work?
As already mentioned everything else (comparator function etc) would be same except now you have to keep track of how much you allocate and it will be passed to the qsort. You are already allocating the amount of memory using the hardcoded value of 4. Make a variable const int size = 4 and then keep it and use it accordingly.
Long story short - problem was in getting the size of the number of elements that you wanted to sort.

There is no error in calloc function, I already checked size in other project, and it stores needed amount of strings. But I still dont understand, how to qsort char**

@MiradilZeynalli.: You can't apply sizeof over a pointer and expect it to return the size of the allocated memory! That's where you were wrong.

Keeping track of the size of array worked. Thanks a lot)

The problem that you are having is with the variable len. When you want to allocate memory via functions as malloc() and calloc(), it is better that you use a variable for the size of memory to be allocated. So instead of writing:
char** s;
s = calloc(4, sizeof(char *));

write:
size_t szAlloc = 4;
char **s = calloc(szAlloc, sizeof(char *));

This way, you can keep track of the allocated memory and you can use this value when needed. As the variable s is declared as a pointer, applying the operator sizeof() on it will yield the size of that pointer, not the size of the memory allocated by calloc(), and dividing it by sizeof(char *) will give 1 as a result.
Using the variable szAlloc you can call qsort() as:
qsort(s, szAlloc, sizeof(char *), cmp);

and this should give a proper result.

In the first case, since s is a pointer, sizeof(s) will give you the size of the pointer, not what it points to. That of course leads to len being equal to 1 (the size of a pointer divided by the size of a pointer).
In the second case s is no longer a pointer, but an array of four elements. It's type is char*[4].
You can only use the sizeof-division "trick" with actual arrays.
If you want to use pointers and dynamically allocated "arrays" you need to keep track of the number of elements yourself.

Is there any way to get len to be needed size for char** case?

@MiradilZeynalli No, you have to keep track of it yourself.

Thanks for helping)
