Function pointers and callbacks

preview_player
Показать описание

In this lesson, we have explained use cases of function pointer through code examples. In previous video, we had explained basics of function pointers.

See Bubble sort video here:

About event handling - You can think of a scenario where if something happens in a user interface, you want to perform some action. For example, upon a mouse click, you want to call some function. Many libraries give you a design where you can register a callback function to be called upon an event (like a mouse click). Library function will callback this function that you would register whenever the event occurs. Basically library function will call all functions registered for the event (stored in some list).

Рекомендации по теме
Комментарии
Автор

You are the best C language instructor I've found on YouTube. Clear and concise.

OzieCargile
Автор

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)

yasserosama
Автор

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!!!

cbeHotboyred
Автор

You explained everything about sorting but little about function pointers and callbacks. Like when a call back is really useful.

baconsledge
Автор

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++)

chandrashekhar
Автор

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...

Blaqueken
Автор

Thanks for separate series on pointer, this helped me in embedded system topic

nawinksharma
Автор

wow this is so understandable if you're a beginner, great job

certified_car_simp
Автор

Very good explanation, 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, \n"
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.

ediccartman
Автор

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.

saivenkat
Автор

Thanks, watched them all.
Easy tot understand the basics of C with these video's.

evertdekker
Автор

This is perfect haha, thank you. Makes so much more sense

flywittzbeats
Автор

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?

alisalamatian
Автор

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.

moyam
Автор

Thank you for your efforts in explaining tricky things so easily.

NishantSingh-ytem
Автор

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.

ericliudev
Автор

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 "

saurabhsoni
Автор

add: 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

chesteringosan
Автор

10:13 why do you have to pass function pointer when you can use the "function compare" directly inside "the function BubbleSort"

shivkrishna
Автор

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.

saeidsaati