-2

I am trying to sort strings using stdlib qsort. I have created two sort functions sort1 and sort2. sort1 input argument is char** and sort2 input argument is char[][]. My program crashes when use sort1 function to sort array of strings.

#include "stdafx.h"
#include <stdlib.h>
#include <string.h>


int compare(const void* a, const void* b)
{
    const char *ia = (const char *)a;
    const char *ib = (const char *)b;
    return strcmp(ia, ib);
}

//program crashes
void sort1(char **A, int n1) {

    int size1 = sizeof(A[0]);
    int s2 = n1;
    qsort(A,s2,size1,compare);
}

//works perfectly
void sort2(char A[][10], int n1) {

    int size1 = sizeof(A[0]);
    int s2 = n1;
    qsort(A,s2,10,compare);

}

int _tmain(int argc, _TCHAR* argv[])
{

     char *names_ptr[5] = {"norma","daniel","carla","bob","adelle"};
     char names[5][10] = {"norma","daniel","carla","bob","adelle"};
     int size1 = sizeof(names[0]);
     int s2 = (sizeof(names)/size1);    
     sort1(names_ptr,5); //doesnt work
     sort2(names,5); //works
     return 0;
}
user968000
  • 1,765
  • 3
  • 22
  • 31
  • One ios a pointer, the other a 2D array. What is your **specific** problem you don't finmd answered in any C book or multiple times already here? – too honest for this site Aug 02 '16 at 00:03
  • 1
    Read e.g. [this old answer of mine](http://stackoverflow.com/questions/18440205/casting-void-to-2d-array-of-int-c/18440456#18440456). – Some programmer dude Aug 02 '16 at 00:03
  • I wanted to know why qsort is not complaining in 2D array , while the program crashed when the array was passed as **. – user968000 Aug 02 '16 at 00:07
  • 1
    @Olaf Which warning are we talking about? I compiled with `-Weverthing` and none of the warnings have anything to do with the actual problem here. – user3386109 Aug 02 '16 at 00:25
  • Section 6 of the [comp.lang.c FAQ](http://www.c-faq.com) has a good explanation of the (admittedly sometimes confusing) relationship between arrays and pointers in C. Rule 1: Arrays are not pointers. – Keith Thompson Aug 02 '16 at 00:28
  • 1
    @Olaf there would be no warnings, since `qsort` goes via `void *`, just silent UB due to casting to the compare function casting to the wrong type in the `sort1` case – M.M Aug 02 '16 at 00:39
  • @M.M: Yes, **that** one. Anyway, nothing not being answered here a hundret times already. OP does not show any own research effort. And `_tmain` is not the correct entry point. What is that Windows? – too honest for this site Aug 02 '16 at 01:11

1 Answers1

2

The qsort function receives a pointer to the thing being sorted. In sort2 you are sorting arrays of 10 char. In sort1 you are sorting pointers to char.

So the compare function is wrong for sort1, because the arguments are pointers to pointers to char (converted to void *), but you cast to pointer to char.

The sort2 works because converting a pointer to array of 10 char into a pointer to char (via void *) produces a pointer that points to the first character of those 10.

You need to use a different compare function for each of these two cases, because you are sorting different things.

M.M
  • 138,810
  • 21
  • 208
  • 365