From the course: C Standard Library
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Sort and search functions - C Tutorial
From the course: C Standard Library
Sort and search functions
- [Instructor] So let's write the compare function in line 10. Now remember that the prototype of this function is already specified in the definition of q search and b search. So it has to return an integer. You can name it however you want. I will call it my compare. And the arguments have to be two void pointers. And they have to be defined with the const qualifier so that's const void pointer. I will call the first one left hand side, and the second one, right hand side. And the implementation will be very simple. I will simply subtract left hand side minus right hand side. But we have to do this in a clever way, so that the compiler knows that we want to subtract integers because remember, these arguments are void pointers so they may be anything. What this means is that I have to cast a lot of variables here. Let me show you how. So I will return a subtraction. Now this subtraction will be, left hand side, with the in direction operator minus whatever right hand side points to…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
Memory allocation1m 32s
-
(Locked)
Memory allocation example2m 29s
-
(Locked)
Random number generation1m 48s
-
(Locked)
Random number generation example2m 40s
-
(Locked)
String conversion2m 11s
-
(Locked)
String conversion example6m 29s
-
(Locked)
Sort and search1m 52s
-
(Locked)
Sort and search main4m 50s
-
(Locked)
Sort and search functions5m 6s
-
(Locked)
Challenge: Let's get random1m 38s
-
(Locked)
Solution: Let's get random5m 40s
-
-
-
-