I have the following code to add directory contents to a malloc'ed array of pointers to arrays.
However, after passing the 2d array to qsort, qsort returns nothing in the array.
array here = (null)
If i take the qsort out, the print_dir() prints the array nicely.
Am i missing something?
Code:int n = 100; char **names_array; names_array = malloc(num_of_files * sizeof(char *)); if(names_array == NULL) { fprintf(stderr, "out of memory\n"); } for(i = 0; i < nrows; i++) { names_array[i] = malloc(n * sizeof(char)); if(names_array[i] == NULL) { fprintf(stderr, "out of memory\n"); } } if ( (dirp = opendir(current_dir)) != NULL ) { i = 0; while ( (direntp = readdir( dirp )) != NULL ) { names_array[i][n-1] = '\0'; strcpy(names_array[i], direntp->d_name); printf("array here = %s\n", names_array[i]); i++; } qsort(names_array,num_of_files ,1024, qsort_contents); printf("array here = %s\n", names_array[0]); print_dir(names_array, num_of_files, 1, flags); }



LinkBack URL
About LinkBacks


