Hi I just need help with this code when ever I try to run this code it says Segmentation fault (core dumped) . I think it could be my compare function . I want to sort to every struct in the array.
Code Removed (Ignore)
Hi I just need help with this code when ever I try to run this code it says Segmentation fault (core dumped) . I think it could be my compare function . I want to sort to every struct in the array.
Code Removed (Ignore)
Last edited by akpr2000; 09-13-2020 at 08:03 AM. Reason: Code Removed
Hard to say where you messed up, when you've deleted your code.
But here's a sequence of exercises.
Then adjust for a structure.Code:int array[5] = { 4, 1, 8, 3, 9 }; qsort( array, 5, sizeof(array[0]), compare_fn);
Then adjust for a dynamic array.Code:struct foo { int a; }; struct foo array[5] = { {4}, {1}, {8}, {3}, {9} }; qsort( array, 5, sizeof(array[0]), compare_fn);
Code:struct foo { int a; }; struct foo *array = malloc( 5 * sizeof(*array) ); // initialise.... qsort( array, 5, sizeof(array[0]), compare_fn);
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.