You are the best C language instructor I've found on KZbin. Clear and concise.
@oleksijm4 жыл бұрын
You are so right.
@yasserosama34057 жыл бұрын
Using a function pointer instead of just calling a comparison function is more useful because then you can create many different comparison functions and still use that one sort function. This means you no longer have to have direct access to the implementation details of the sort function, thus helping to keep things cleaner and easier to see/manage. (this is not my comment , it's a reply to a question 3 years ago from now , I pasted it so new people coming could see it and maybe help them understand the real benefit/use of function pointers)
@cbeHotboyred16147 жыл бұрын
Man !!! You explain everything so great. I was so confused on this topic before your video. I had a similar program like this and need a way to change the comparison operator like < ,> !=, and == , but I didn't know how!! So i always just wrote duplicate code to change just one thing in it. Thanks, you explain everything perfectly!!!
@baconsledge2 жыл бұрын
You explained everything about sorting but little about function pointers and callbacks. Like when a call back is really useful.
@Blaqueken8 жыл бұрын
The best online teacher on the Internet so far... as I'm concerned. Bravo! These concepts flew over my head back in class but now i understand. What a talent this guy. Very inspiring...
@nawinksharma6 жыл бұрын
Thanks for separate series on pointer, this helped me in embedded system topic
@chandrashekhar96935 жыл бұрын
In Bubble sort program (4:42), unnecessary looping we can use for( j = 0; j < n - ( i + 1); j++) instead of ( j = 0; j < n -1; j++)
@evertdekker39159 жыл бұрын
Thanks, watched them all. Easy tot understand the basics of C with these video's.
@jianxiaohu82134 жыл бұрын
This eps is another level!
@shivkrishna61579 жыл бұрын
10:13 why do you have to pass function pointer when you can use the "function compare" directly inside "the function BubbleSort"
@Phantom10377 жыл бұрын
Inside the main function when we called the bubble sort function, we gave as arguments : ( A, 6, compare). - A is an array and it returns the address of the first element, so the function declaration of bubbleSort should have the first argument as an pointer to iteger. - 6 is an integer so the second argument of the bubbleSort function should be an integer. - compare is a function and it returns the address of the compare function, so we need to pass a pointer to function as the third argument to the bubbleSort function.
@Arjunreddy-xi5ht7 жыл бұрын
if there are more than 1 compare functions,instead of changing the code everytime you can just use a function pointer to call the required function and make the required comparison.
@KshitijKale4 жыл бұрын
My guess, it has something to do with the scope of the function. Since all the functions in the given example are global; they can be called effortlessly anywhere. But if some function was inside the main or any other function then it wouldn't be possible to directly call the function and hence a function pointer should be used.
@KshitijKale4 жыл бұрын
Ok, this one makes more sense. In the example of sorting integers using a pre-built quick sort algorithm we directly use the function. But since the definition of the actual function is not provided instead an extra parameter is allowed to be defined by the user himself so that they can still use the algorithm for quicksort and also make their own ranking function. In this way the source of code of qsort is safe and functionality is also maintained. If instead, we couldn't pass the function pointer then we would have to directly alter the qsort algorithm which in this case might even be possible but many times the source code isn't available. I hope this helps after this many years and please do share any insight that you might have discovered.
@ediccartman72528 жыл бұрын
Very good explanation, BUT.......For summary it would be useful to bring the code with both cases , like : void main() { int order, counter, a[ SIZE ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 }; printf( "Enter 1 to sort in ascending order, " Enter 2 to sort in descending order: " ); scanf( "%d", &order ); ................................. if(order==1) Bubble(a,size,ascending); .......................................................... else Bubble(a,size,descending); And then it's clear, that in the1st case the function pointer will point to ascending , and in the 2nd - to descending.
@shubhamsuraj27254 жыл бұрын
dil jeet liya bhai ye video :)
@certified_car_simp1856 Жыл бұрын
wow this is so understandable if you're a beginner, great job
@NishantSingh-yt9em4 жыл бұрын
Thank you for your efforts in explaining tricky things so easily.
@ericliudev9 жыл бұрын
Hi, this is an amazing series of tutorials. The pointer to function is really interesting, however, language like Java doesn't have this mechanism. I guess we will have to use 2 classes to implement an interface and pass the interface as a argument to the method to achieve the same design.
@yuvraj72143 жыл бұрын
Java does have pointers.
@moyam019 жыл бұрын
What is the difference between using the function pointer, and calling the compare function directly? for example, if within the sort function you just call compare(), and return the result. I've done something similar in Visual Studio, and it compiled and worked.
@lautarodelucia64807 жыл бұрын
What comes to mind is that you cannot change the sorting order from main in that case. By using function pointers you could define two compare functions, one for increasing and other for decreasing order. They would only differ in the order of the returns ( swap 1 for -1). Then you can use command line arguments, for example, to choose your sorting order without touching the code.
@milanpatel31595 жыл бұрын
the thing here is that if you don't have write permission to the BubbleSort() definition you can change sorting behavior from the main function. No need to touch BubbleSort()
@saeidsaati40214 жыл бұрын
Great tutorial with a good explanation thank you, I just think that in 14:34 const is using for preventing to change the "value" of the pointer not the address of it.
@AmitYadav-rp3ot4 жыл бұрын
Correct!
@nikhilk74934 жыл бұрын
9:29 you replace n-1 with i in inner loop
@saurabhsoni50477 жыл бұрын
Good example used. Thumbs Up for that.. Although, Regarding the compare function prototype for qsort in the last where you make the statement "const keyword is used here so that you cannot change the address stored in this pointer variable" It should be " cannot change the dereferenced value for that pointer variable "
@flywittzbeats40087 ай бұрын
This is perfect haha, thank you. Makes so much more sense
@alisalamatian24183 жыл бұрын
Thanks for all of your videos. I really find them useful. I just still do not understand why we should use a function pointer when we can directly call the function. Could someone explain the difference to me?
7 жыл бұрын
It was really helpful. Thank you for the tutorial.
@rd-tk6js6 жыл бұрын
Thanks, excellent series on pointers !
@saivenkat3 жыл бұрын
Lets say you are designing a library function that would do something like sorting so anyone can just use your library function and sort. And you want to keep this sorting function generic, so data type can be anything. Then, how would you take the required information to sort from user of your library function? You cannot guess the comparison logic for a complex daya type. So, you would want to get the comparison logic too which can be passed as a function. So, in many cases when we are dealing with generic scenarios, function pointers make our life easier. It should be seen as a way to accept functionality as argument. So far, we knew that functions can accept only data, but now we know that even a function can be passed.
@rmarinero5 жыл бұрын
I understood this within the first and a half minute, amazing and clear explanations!
@himanshusingh-sh7ih10 жыл бұрын
You made my concept very clear! Thanx a ton :)
@emmanuelakpabio9529 Жыл бұрын
This is super duper amazing!😊
@ToanPhan038 жыл бұрын
At 11:04 why we dont need to change *compare to *absolute_compare in void BubbleSort(.....) function. Also compare(A[j], A[j+1]) to absolute_compare(A[j], A[j+1]). But it still works fine because the value store in memory where pointer *compare point to?
@ankan20888 жыл бұрын
nice observation. The bubble sort if you notice carefully takes the argument (A, 6, absolute_compare). So the int (*compare)(int, int) declared inside the void BubbleSort(int *A, int n, int (*compare)(int, int)) points to absolute_compare function instead of the compare function. Even if you changed the compare inside BubbleSort to another name( say, int (*ptr)(int, int)) it would work fine. I hope I explained it clearly. :)
@nakulnair4977 жыл бұрын
I still didn't understand why *compare wasn't changed to *absolute_compare in bubblesort func definition
@ibiixie8 жыл бұрын
Had no idea this was possible, this sure will make my GUI system a lot easier to create! :>
@venkyreddy550710 жыл бұрын
Awesome explanation bro.I really wondering that how could you do that.
@abhimanyumishra94559 жыл бұрын
Amazing Explanation!!
@prankurhauhan6 жыл бұрын
Hi, Thanks for the explanation it was great!!. just a remark/question: at 14:33 you said const void* a , we cannot modify the address that 'a' points to , which means you are implying 'a' is a constant pointer, but I think it is that the variable pointed by 'a' cannot be modified, means the *a = 3 is not allowed and a = &someOtherVariable is allowed.
@antilogism5 жыл бұрын
The "const" qualifier has the same effect on a pointer as it does on any other variable. Pointers, like a and b, are just variables containing an address and, with that qualifier, must always have the same value. However the data stored at those addresses are not bound by the qualifiers on those pointers. The data will follow the declarations that allocated the storage in qsort().
@AmitYadav-rp3ot4 жыл бұрын
@@antilogism Pointer to constant { const int *ptr } This indicates that the content at the address pointed by ptr cannot be changed, but changing the address pointed by ptr is allowed to change. Constant pointer to variable { int *const ptr } This indicates that the address pointed by ptr cannot be changed, however the content of the address is allowed to change. source: www.geeksforgeeks.org/const-qualifier-in-c/
@Littleangel130611 жыл бұрын
Thanks man your explanations are really good..........I appreciate you.
@darshilmodi68885 жыл бұрын
brillaint example and amazing explaination !! Keep going
@amalkms10 жыл бұрын
yours videos are really helpful , thank you very much ....
@syfaiz5 жыл бұрын
Splendid explanation!
@MrJayesh059 жыл бұрын
Brilliant job. Keep it up guys.
@Heizler6 жыл бұрын
Very good explanation! Thanks!
@chesteringosan60394 жыл бұрын
add: bubblesort(...){...for(...){int x =0;...for(...x += 1;){...}...if(x==0){break;}}} from a newbie with c, this additional statement will break when the array is already arranged, because the example from the video will loop as many as indicated in the arg, even if the array is already sorted
@chesteringosan60394 жыл бұрын
add the x += 1 inside the if(A...){....here>>x++;}
@ahbarahad32034 жыл бұрын
A very interesting use of function pointers that i have seen in real life projects is in Event Handling while passing delegates/function signatures to functions, under the hood its basically the same thing except the delegates also have an iterator.
@ahbarahad32034 жыл бұрын
Oh he mentions it at the end of the video, lol
@XieQiu8 жыл бұрын
thank you for posting this.
@bibinio10011 жыл бұрын
Thanks a looooooooooooooooooooooooooooooooooooooooooooooooot of you are a wonderfull teacher !!!!!
@jackofnotrades155 жыл бұрын
Hi, I need a bit of clarification about what you said at 14:38 about function parameter being const. You said it is passed as const because the address shouldnt be modified. But does it signify the address not being modified or value inside the address not be modified? Basically whats the difference between const void *p and void* const p?. Please reply
@Rohitsingh24105 жыл бұрын
He is dead bro. yourstory.com/2014/06/techie-tuesdays-humblefool
@jackofnotrades155 жыл бұрын
@@Rohitsingh2410 I didnt knew that. May his soul rest in peace. And may be someone notice my comment and respond. May be he himself in someway.
@AmitYadav-rp3ot4 жыл бұрын
///////////////////////////////////////////////////////////////////////////// Pointer to constant { const int *ptr } This indicates that the content at the address pointed by ptr cannot be changed, but changing the address pointed by ptr is allowed to change. ///////////////////////////////////////////////////////////////////////////// Constant pointer to variable { int *const ptr } This indicates that the address pointed by ptr cannot be changed, however the content of the address is allowed to change. source: www.geeksforgeeks.org/const-qualifier-in-c/
@MustafaTaqi11 жыл бұрын
Thanks a lot, you are really doing a great job
@harshith_takkala2 жыл бұрын
Awesome one
@manjunath.r151311 жыл бұрын
it is really good sir thank you sir!!!!!!!!! keep going with your good job!!!!!!!!!!and help student like us.!!!
@anurag87258 жыл бұрын
Why are we using a separate compare function when we already have compare (< or >) in our bubblesort function. How are we using this compare in bubblesort function along with"". Where is the user input flag you were talking about.
@billigerfusel8 жыл бұрын
Wooot this is awesome explanation
@MartiinCarrillo5 жыл бұрын
Great explanation!! Thanks a lot!
@adithyashankar65627 жыл бұрын
Good explanation on callbacks
@anghelutadragos18989 жыл бұрын
Could you list some exercices for this playlist about pointers ?
@Addu19026 жыл бұрын
Please
@hirokaanf428710 жыл бұрын
why the first program (from the beginning until 2:39) is not working id u keep return 0; in the main function?
@mycodeschool10 жыл бұрын
return 0 is main function is not forced by most compilers. Mine does not. So, often we do not write it.
@kanwarpandit87984 жыл бұрын
Bhai. Kmaal Kar diya
@souravsingh7634 жыл бұрын
I was so confused with callback function working in JavaScript As i am familiar with c/c++ this callback in c and in JavaScript are same Thanks 👍
@mehulshah847010 жыл бұрын
can u tell me the advantages of function pointer over calling function directly ?
@whydiswhydat2 жыл бұрын
where are the arguments being passed from into the comparison function? I think form actual A[ ] right? If so, is it just comparing the first two numbers, 3 and 2, and proceed from there?
@jamesward29467 жыл бұрын
This is great stuff!
@adityakrverma61210 жыл бұрын
Question to "mycodeschool" : this is really good video. Thanks for explaining. but question is how we are doing callback. It looks like regular calling of function when you call 'compare' inside bubblesort. Is that you mean is callback ? Can you please elaborate on callback. Also in the end of video you mentioned about callabck and event handling. That's what i am interested in. Is there any video or link on that ? Thanks !
@harikumar-cq1je3 жыл бұрын
I had the same question too. What's the difference between calling a function pointer and a normal function? I feel both do the same right?
@sxve-x Жыл бұрын
8 years on and I have same question.
@nishanthkr28385 жыл бұрын
Thats some high level thinking there.
@roshenw11 жыл бұрын
Thank youu soooooooo much really clear and helpful!!!!
@Jonathan-ru9zl2 жыл бұрын
thank u . keep up the good work
@cbeHotboyred16147 жыл бұрын
Do pointer function release its memory like the a regular function does when it finish executing OR do i have to delete it. If i have to delete it, How?? Is it possible to delete the pointer function ...... like the way you delete the pointer variable in c++.
@nickeax4 жыл бұрын
Amazing, thank you very much.
@anmoljain10123 жыл бұрын
you are most welcome!
@BYugandhar7 жыл бұрын
Thanks a lot sir for providing good explanation, but I don't get one thing while calling compare form main. From where it's taking
@dawoodfarooq36058 жыл бұрын
Amazing. Keep up the good work (Y)
@vaibhavsingh41084 жыл бұрын
@10:50 the function Bubblesort takes argument as (int array, int n, int *compare (int,int) but when in main we call this funtion we pass argument as (array , n, absolute_compare ); which is another function . How this is happening , I mean we can how we can pass this . Anyone please help
@chaitanyabade91264 жыл бұрын
Technically, absolute_compare works as an actual parameter and int *compare (int, int) work as a formal parameter, So if you pass any function as a parameter it will work until and unless the signatures are the same, different names doesn't matter.
@vaibhavsingh41084 жыл бұрын
@@chaitanyabade9126 thanks
@aadeshmehta7 жыл бұрын
Hi, Thanks for the video. You explain really good. Which editor have you used? It seems really clean and helpful with function definitions and syntax. Are there any other such editors you recommend
@harikumar-cq1je3 жыл бұрын
Even though the explanation is awesome. Still, I don't understand What's the difference between calling a function pointer and a normal function? I feel both do the same right? Even though you call Compare as a normal function that also does the same right? doe that makes any difference internally through memory?
@ChienChiWang9 жыл бұрын
Hi~~ I have a question. What are the variables the void pointer a and b in the function compare point to, after the program executes the function qsort ?? Will they point to elements in array A?? And the effect of return value of compare is to swap the two values in the elements?
@dn92555 жыл бұрын
I don't get it. So why do we need to declare function pointers if we can just call the actual function like you did in the last example?
@VikasPoonia5 жыл бұрын
In other programming languages we need this, like when an application waits for data and other things should be carried out. Like on KZbin, when you are watching a video, the next byte of the video should be downloaded. Suppose we have two function nested.. The first start the video and the nested one download the next byte of video to be seen. So the first executed itself but the second one is still waiting in memory for its required data. I hope you got it. We have in JavaScript: callback functions, async await and promises works on similar principles. function 1===> I am doing this. Function 2 ==> I am waiting for the next byte will return this as soon as I received it
@ahmed_raaphat Жыл бұрын
BRILLIANT
@werewolf_134 жыл бұрын
Explanation was good. Thanks! Just would like to suggest a correction in the bubble sort algorithm .
@utkarshjha657 Жыл бұрын
In the bubble sort if-expression, can we write :- if(compare(*(A+j), *(A+j+1) > 0) ?
@Praveenkumar41219948 жыл бұрын
tanx a lot!!! it was great help!!!!
@SuperSayiyajin5 жыл бұрын
Hi.Thanks for videos.I have a question. Is there any method or function to take array size? You typed Bubblesort( A,6 ); line. Can we use anything instead of 6?
@martianpirate96574 жыл бұрын
11:30 --> qsort
@rationalisthuman31589 жыл бұрын
why need of callback mechanism when we we can call the compare function directly @ 7:00
@billigerfusel8 жыл бұрын
Because a library function don't know what your compare logic function is called.
@raj714417 жыл бұрын
I didn't understand why library function knows our compare function please tell.
@ognjen2972 жыл бұрын
God bless you !
@dumamageswarisitaraman95014 жыл бұрын
Is it possible to apply pointer arithmetic operation with function pointer.
@sureshdharavath119110 жыл бұрын
with out using the callback can't we call the compare function from the if condition so that we no need to pass the address from the calling function. Is there any problem in this ?
@VV-cy9gf10 жыл бұрын
Using a function pointer instead of just calling a comparison function is more useful because then you can create many different comparison functions and still use that one sort function. This means you no longer have to have direct access to the implementation details of the sort function, thus helping to keep things cleaner and easier to see/manage.
@srujankumar19998 жыл бұрын
+AMG POWER when we use function pointer...does it creates a slot for comparison function,above sort function on stack..for every comparision( comparing A[j],A[j+1] in a loop untill j
@yasserosama34057 жыл бұрын
this explanation has solidified my understanding of function pointers and their use , thank you sir ! , please don't mind if I copy and paste your comment , as it would be new for people watching this video now , so they could easily see it and maybe help them understand like I did :)
@Arjunreddy-xi5ht7 жыл бұрын
AMG POWER Nice explanation..it helped me understand the actual use of a function pointer,ThankYou.
@bradmorgan608 ай бұрын
Thank you so much.
@dickensowuor9091 Жыл бұрын
Good job
@tarunkumar75637 жыл бұрын
code that you write to explain the concept ,is the syntax is same in both c and cpp
@pvsrinivas10010 жыл бұрын
very informational thank you.
@amarnathnayak11458 жыл бұрын
can someone explain me the last program, where he pass "tom" string to called function but dereferenced it by 'name' while printing. why didn't he write *name?
@lifesmysteriesofficial5 жыл бұрын
4:42
@MrNams7 жыл бұрын
thanks
@luqmanahmad792911 жыл бұрын
Great yaar
@Kapil_Thakar10 жыл бұрын
great dear. useful.
@anshulsoni47710 жыл бұрын
I still dont get, it ! Using the flag would have been lot more easier, so why use a call back instead of that ?
@mycodeschool10 жыл бұрын
Lets say you are designing a library function that would do something like sorting so anyone can just use your library function and sort. And you want to keep this sorting function generic , so data type can be anything. Then, how would you take the required information to sort from user of your library function? You cannot guess the comparison logic for a complex daya type. So, you would want to get the comparison logic too which can be passed as a function. So, in many cases when we are dealing with generic scenarios, function pointers make our life easier. It should be seen as a way to accept functionality as argument. So far, we knew that functions can accept only data, but now we know that even a function can be passed.
@anshulsoni47710 жыл бұрын
mycodeschool Cool got it, thanks
@akashpatel554710 жыл бұрын
mycodeschool but i did not get it. it will good if you give small code example.
@anshulsoni4779 жыл бұрын
mycodeschool So wait I just realized this going over this answer that if I am passing compare for generic data type the function declaration for BubbleSort cant be void BubbleSort(int* A,int n, int* compare(int,int)); it has to be void BubbleSort(T* A,int n, int* compare(T,T));
@anshulsoni4779 жыл бұрын
Akash Patel According to what I understand this is an example struct vector { //Definition of struct friend bool operator>(vector& a,vector& b) { //Vector comparison logic here } }; template int compare(T a, T b) { //Same Logic } template void BubbleSort(T* a, int n, int(*compare)(T, T)) { //All the logic would stay the same //Except T temp; //instead of int temp; } int main() { //Declare an array of vectors; BubbleSort(A, 10, compare); }
@aakash43517 жыл бұрын
math.h not working for me, when i try to use abs. Instead of it i have to use stdlib.h.
@muthusamy45010 жыл бұрын
were is the link for event handling?? can you please provide the same.. thanks!! the vedio was very good :) :)
@mahimajangra32484 жыл бұрын
why we declare function variable with "(*ptr) ();" and use it as "ptr();" .... [without "*" ]
@jamestheking98163 жыл бұрын
Because when you call any function, say add(), under the hood the CPU saves current address of where the program is, jumps to the *address* of the add() function, executes the statements in there, then jumps to the return address where the program left off. So, calling a function really just means “jump to the address of add() subroutine and execute those statements.” The C compiler knows when you declare a function pointer and use “*ptr”, you are dereferencing it, meaning you want to call the function. But as discussed, ptr() is *ptr() is the same thing, I.e. they mean the same thing
@nikhilanand9844 жыл бұрын
for compare function pointer in BubbleSort why didn't we passed the address of the compare function instead we directly passed the arguments.
@abdelrhmanahmed13784 жыл бұрын
Are this is what happen we do run time Polymorphism is just function pointer using vtable and vrpointers !?
@TON-vz3pe4 жыл бұрын
So, basically function pointers are the same as method calling in java using public, private
@ijazmuhammed78223 жыл бұрын
Than you so much
@stevefrt94958 жыл бұрын
Thanks you so much
@mrpossible56965 жыл бұрын
12:50
@sudeshnaguharay5187 Жыл бұрын
why in bubblesort func, the name of argument not changed to absolute_compare, still program is running fine?