qsort

Understanding qsort() function

瘦欲@ 提交于 2019-12-12 06:27:08
问题 int compare (const void * a, const void * b) { return ( (int) (*(float*)a - *(float*)b) ); } When I want to use qsort function, I should write a compare function in this form as I understood. So, why we are using void before the parameters of compare function? And, is this compare function standard with its parameters and code in curly braces or can we simply change the code and even parameters through our wishes? 回答1: No, you can not change the parameters or the return type. But of course

c qsort string array

倾然丶 夕夏残阳落幕 提交于 2019-12-12 03:09:34
问题 qsort(words, size1, size2, compareWords); inside compare words: int compareWords(const void *ac, const void *bc) this works: char const *a = *(const char **)ac; these don't ( a gets some garbage values): char const *a = ac; char const *a = (const char *) ac; what is the rationale? Also, in some examples I see size2 to be sizeof(char *) . Shouldn't this be sizeof(*words) ? words is declared as: char *words[] = {"abc", "pqr", "abcd", "pqsl"}; 回答1: When qsort ing an array of T, your comparison

qsort won't sort dynamically allocated array of structs

允我心安 提交于 2019-12-12 01:39:37
问题 I have a struct struct info { char firstName[100]; char lastName[100]; char companyName[100]; char email[100]; unsigned long phoneNumber; }; which is stored in the file compareElements.h I read in a set of values into a dynamically allocated array of structs called bptr. my comparePtr points to this function. #include <string.h> #include "compareElements.h" int compareNameAscending (const void *a, const void *b) { struct info *part1 = (struct info *) a; struct info *part2 = (struct info *) b;

Sorting 2 large arrays

拈花ヽ惹草 提交于 2019-12-12 01:12:11
问题 I have not yet taken data structures and algorithm class and I am having some troubles with what I try to do. I have 2 large arrays, 1 is char with about 80k-100k words and second is int array with same amount of integers (for example, word written in words[502] has its number of occurences written in integers[502] ). I have to sort them so the largest integer with it's corresponding word is 1st, second largest 2nd etc etc, is it possible without using bubble sort (which is too slow for these

Sorting dynamic array in a structure with C qsort ()

为君一笑 提交于 2019-12-11 18:07:57
问题 I have a problem to sort an array (dynamically allocated) in a structure. Firstly, the idea was to order the array i in the structure in an ascendant order. Then I was thinking to order the array i maintaining instead the array j with the same "relationship" obtained when it was constructed the initial structure. I try to work for the first idea, but without any result with qsort.So this is my code... Any ideas? I think there is a problem in the construction of the comparing function..

Array-strings sorted using qSort in C

早过忘川 提交于 2019-12-11 14:24:47
问题 the question is simple: there is some way that the ordered array that returns me the "qsort", is returned in reverse, ie I want to avoid the use of any auxiliary array to invest the resulting array using qsort. this is my code, which reads from standard input strings to be sorted, and uses a comparison function for sorting. #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <stdio.h> #include <string.h> int cstring_cmp(const void *a, const void *b) { const char **ia = (const

Warning while sorting a 2D array with qsort

血红的双手。 提交于 2019-12-11 13:37:40
问题 I'm trying to use qsort to sort a 2D array in C. The sort works, but I get the warning: warning: initialization discards 'const' qualifier from pointer target type [enabled by default] How can I modify my compare function to eliminate the warning (given that qsort requires the parameters const void *pa, const void *pb ? int cmp (const void *pa, const void *pb ) { const int (*a)[2] = pa; // warning here const int (*b)[2] = pb; // warning here if ( (*a)[1] < (*b)[1] ) return 1; if ( (*a)[1] > (

Typecast for qsort function pointer

强颜欢笑 提交于 2019-12-11 13:37:05
问题 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h> static int cmpstringp(const void *p1, const void *p2) { /* The actual arguments to this function are "pointers to pointers to char", but strcmp(3) arguments are "pointers to char", hence the following cast plus dereference */ return strcmp(* (char * const *) p1, * (char * const *) p2); } int main(int argc, char *argv[]) { int j; assert(argc > 1); qsort(&argv[1], argc - 1, sizeof(argv[1]), cmpstringp); for (j = 1; j

Why don't you need to pass arguments to a qsort comparator function?

和自甴很熟 提交于 2019-12-11 08:12:03
问题 Code below taken from here. * qsort example */ #include <stdio.h> #include <stdlib.h> int values[] = { 40, 10, 100, 90, 20, 25 }; int compare (const void * a, const void * b) { return ( *(int*)a - *(int*)b ); } int main () { int n; qsort (values, 6, sizeof(int), compare); for (n=0; n<6; n++) printf ("%d ",values[n]); return 0; } We have a compare function with parameters in its signature but when we call it in qsort no arguments are passed. How are the values of a and b passed to the function

qsort and bsearch an array of pointers

∥☆過路亽.° 提交于 2019-12-11 02:54:28
问题 I need to sort an array of pointers to struc. In fact, I need to do searching among adresses to see if a given pointer to a struct is present in the array. Unfortunately, I don't have nothing "comparable" inside those structures and so I want to sort'em just by address. My code is like that: item* arr[SIZE]; //something is inserted qsort(arr, SIZE, sizeof(item*), (void*)compare_funct); //CUT bsearch(curr, arr, SIZE, sizeof(item*), (void*)compare_funct); I tried creating a compare_funct just