you explained this so much better than my prof. prof puts me to sleep.
@PortfolioCourses Жыл бұрын
Hahaha I'm sorry your prof puts you to sleep! I'm glad this video was able to help you out! :-)
@madmademoiselle097 Жыл бұрын
thank you, a clear, easy to understand tutorial!
@PortfolioCourses Жыл бұрын
You're very welcome, I'm glad you found it easy to understand! :-)
@gammyhorse2 жыл бұрын
So basically, if I understood correctly, the 4th argument of qsort is a function pointer returning an int, which is very interesting if this is the case. Function pointers are powerful and I would like to know more about their use in practice. Thank you.
@PortfolioCourses2 жыл бұрын
Yes, exactly, it's a pointer to a function which returns an int. :-) You may have already seen this video on function pointers but I thought I'd share it just in case: kzbin.info/www/bejne/nJDYiIKNi86MmZI
@gammyhorse2 жыл бұрын
@@PortfolioCourses Thank you very much!
@PortfolioCourses2 жыл бұрын
You’re welcome! :-)
@PortfolioCourses2 жыл бұрын
@@johngormley1394 That's very cool John, thanks for sharing, definitely a good application of function pointers. 🙂
@decumon Жыл бұрын
thanks from russia, first video about comparator that i found
@PortfolioCourses Жыл бұрын
You're welcome! :-)
@ongayijohnian8787 Жыл бұрын
I still dont get how qsort works with the x and y, does it iterate in the loop assigning a digit x and its successor y?
@akshatkhatri1167 Жыл бұрын
great video so if we want to use it with array of structures instead of integer array; how exaclty will the casting be done i m a bit confused
@PortfolioCourses Жыл бұрын
It’s done pretty much the same way, maybe the example in this link can help you out? :-) iq.opengenus.org/qsort-in-c/amp/
@ismbks2 ай бұрын
does the compare function has to take const void pointer as parameters? or could it also take another type like const int pointer, so we don't need to use pointer magic and keep the code a bit more simple?
@philipphortnagl2486 Жыл бұрын
I don't get how the compare function works without giving any arguments. I mean you say, we assume the numbers "2" and "5" from the array, but how is this actually working in the function without explicitly giving any arguments for the function?
@PortfolioCourses Жыл бұрын
Great question Philipp! :-) So when we pass compare to qsort() like this: qsort(a, length, sizeof(int), compare); We aren't actually *calling* compare() in that moment. We're actually passing the function itself as an argument to qsort(). And then at a later point the qsort() function will be what actually calls compare()! It will call it and provide it with the arguments to compare, and it will use the return value of the compare() function to help sort the array. We call this a "callback function": en.wikipedia.org/wiki/Callback_(computer_programming). It is a bit unusual because normally we pass values to functions, like 2.5, etc. Technically what is being passed is the memory address of the compare function, and qsort() is able to use that to call the function. In C we call that a function pointer, there is a video on the concept here: kzbin.info/www/bejne/nJDYiIKNi86MmZI.
@pousemad Жыл бұрын
amazing video, thank you so much!!
@PortfolioCourses Жыл бұрын
You're welcome, I'm very glad you enjoyed it! :-)
@kianoushmehrandish78182 жыл бұрын
have you created a same video for sorting structs?
@PortfolioCourses2 жыл бұрын
No, but that's a great idea Kianoush, maybe I can do a video on that one day though. :-) It would be very, very similar to what we've done in this video though. The difference would be here: int x = *(int *)x_void; ...instead of a type cast using int, it would be a typecast using the struc type. So instead of 'int' it should be something like 'struct name_of_struct'.
@ishanrana84022 жыл бұрын
Thank you this was very helpful
@PortfolioCourses2 жыл бұрын
You’re welcome Ishan! :-)
@rishabhpandey12aroll7.59 ай бұрын
Thanks, this really helped
@demiancoorey2394 Жыл бұрын
First class tutorial
@PortfolioCourses Жыл бұрын
I’m glad you enjoyed it Demian! :-)
@nguyentuan-ef5hr6 ай бұрын
Hmm why when you read the array and call qsort you can know is 2-7 but not 7-2 ur because you read the array from a[0] not form the last p/s : sorry if my english is bad
@Qwerty_945 Жыл бұрын
why not x = 7 and y = 2 ?
@varaddeshmukh1137 Жыл бұрын
i am trying to sort ' ' ' float array[5]= {11.1f, 11.34f ,11.22f, 11.99f, 11.00f}; ' ' ' it won't work in qsort why? any solution if i want to sort float using qsort?
@PortfolioCourses Жыл бұрын
Good question Varad! :-) qsort() should work with floats, so it makes me think something else is happening in your code, or maybe how qsort() is being used.
@varaddeshmukh1137 Жыл бұрын
@@PortfolioCourses is there any way to sort float using qsort()
@PortfolioCourses Жыл бұрын
Yes, the 2nd post here has an example: cboard.cprogramming.com/c-programming/1293-qsort-doesnt-work-floats.html. :-)
@LisaDunleavy-q8oАй бұрын
Hackett Oval
@SailsburySaxon-j1o2 ай бұрын
Morar Stravenue
@fridaaa02 жыл бұрын
THANKS
@PortfolioCourses2 жыл бұрын
You're welcome! 😀
@RuthPerez-t3wАй бұрын
Lawson Lane
@MariselaCrelia-t1eАй бұрын
Schiller Vista
@DoyleKim-v7d2 ай бұрын
Jayne Trail
@YolandaPotter-t1d2 ай бұрын
Batz Throughway
@AlexSeals-j8sАй бұрын
Wisoky Prairie
@PierreStudzinski-r3yАй бұрын
Garrison Key
@ChereGiandomenico-l8n2 ай бұрын
Roberts Lodge
@tristanhurley90712 жыл бұрын
beautiful
@PortfolioCourses2 жыл бұрын
Thank you Tristan! :-)
@tristanhurley90712 жыл бұрын
@@PortfolioCourses No worries! Quick question: I am trying to order an array of structs by their Age. In your example when calling the qsort function you use "compare" as the last argument, but why do you not need to provide two inputs (x and y) into your compare function? Yours runs with just 'compare'. Apart from this, I think comparing the ages within the structs should be straight forward?
@PortfolioCourses2 жыл бұрын
Great question. :-) So it's actually the *function* compare that is being supplied as an argument, we're not actually *calling* the compare function there so we don't supply arguments to it. The qsort() function will "have" the compare function as a parameter now, and *it* will actually call the compare function with different arguments to sort the list. And comparing ages within structs should be pretty similar. You'll need to some kind of different casting here though I suspect: int x = *(int *)x_void; int y = *(int *)y_void;