Explain this code. | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
0

Explain this code.

#include <stdio.h> #include <stdlib.h> int compare (const void *, const void *); int main() { int arr[5] = {52, 23, 56, 19, 4}; int num, width, i; num = sizeof(arr)/sizeof(arr[0]); width = sizeof(arr[0]); qsort((void *)arr, num, width, compare); for (i = 0; i < 5; i++) printf("%d ", arr[ i ]); return 0; } int compare (const void *elem1, const void *elem2) { if ((*(int *)elem1) == (*(int *)elem2)) return 0; else if ((*(int *)elem1) < (*(int *)elem2)) return -1; else return 1; }

7th Mar 2020, 12:34 PM
Harshit Nema
Harshit Nema - avatar
4 Respostas
+ 1
The code sorts an array using c library function qsort and prints the sorted array. qsort is a function that sort a given array by user defined order. It has following parameters: 1st parameter is array to sort. Since it can be of any type, this argument has type void*. Any pointer can be casted to void*. In this code 1st argument is arr. 2nd parameter is number of array elements. It is calcaulated at compile time: num = sizeof(arr)/sizeof(arr[0]); It is calculated as follow: sizeof(arr) = 5 * sizeof(int), sizeof(arr[0]) = sizeof(int) num = 5 * sizeof(int)/sizeof(int) = 5 3rd parameter is the size of a single array element. In this code it is sizeof(arr[0]) = sizeof(int) = 4. 4th parameter is a pointer to user-defined function that compares two elements of the sorting array. This function is called repeatedly by qsort. This function should have two parameters that are pointers to comparing elements of the array. The function should compare that elements and return next value: <0 if the element pointed to by 1st parameter goes before the element pointed to by 2nd parameter 0 if element pointed to by 1st parameter is equivalent to the element pointed to by 2nd parameter >0 if the element pointed to by 1st argument goes after the element pointed to by 2nd argument In that code function to comapre elements is compare: int compare (const void *elem1, const void *elem2) { if ((*(int *)elem1) == (*(int *)elem2)) return 0; // if elements are equal return 0 else if ((*(int *)elem1) < (*(int *)elem2)) return -1; // if first element is less than second return -1 else return 1; // if first element is greater than second return 1 } *(int *) elem1 means cast void pointer elem1 to int pointer (int*) and dereference it. After sorting all 5 elements are printed using for-loop.
7th Mar 2020, 1:25 PM
andriy kan
andriy kan - avatar
0
andriy kan brother thank you very much, I understood it clearly now.
7th Mar 2020, 1:42 PM
Harshit Nema
Harshit Nema - avatar
0
andriy kan I have a question, from where the compare will get the value of elements of array?
7th Mar 2020, 2:19 PM
Harshit Nema
Harshit Nema - avatar
0
Spirit, qsort gives that values to compare function in the loop. qsort invokes compare function with two pointers to comparing elements of the sorting array when it need to know current two elements are sorted or not. Depending on the return result of comparing function qsort moves compared elements to approriate positions (swaps them if result is greater than 0). qsort uses quicksort alogirthm to sort elements.
7th Mar 2020, 2:31 PM
andriy kan
andriy kan - avatar