Why are function pointers useful?

  Переглядів 27,894

CodeVault

CodeVault

3 роки тому

Source code can be found here:
code-vault.net/lesson/cvyuqr7...
===== Support us through our store =====
code-vault.net/shop
===== Check out our website =====
code-vault.net
===== Check out our Discord server =====
discord.code-vault.net

КОМЕНТАРІ: 48
@datenshiddd
@datenshiddd Рік тому
2 days of watching videos about pointers, and I found THIS GEM. Watching with mindset of "I want to see in practice, how pointers actually work and what makes them superior over working with variables", you Sir, actually did a job on me. Despite learning coding on my own just to learn something new, I hit a several walls in Cpp, C#, Python where I had problem on passing methods inside methods. Thanks a lot for this gem!
@lag2170
@lag2170 3 роки тому
We needed this for one of our tasks today, you saved our bums :D
@primemaniadrive
@primemaniadrive 3 роки тому
we really did need this our book didn't explain anything
@aldekorea
@aldekorea Рік тому
I was struggling to understand the use cases of pointers and I need this kind of real use in order to be motivated about learning a concept, so your video was a good help. Thanks for your effort and explanation.
@gregtasi
@gregtasi Рік тому
The way you type is so satisfying!
@davidliverman4742
@davidliverman4742 Рік тому
Not many people talk about this. Function pointers are exciting! God bless you for doing this!!
@karimjerbi7084
@karimjerbi7084 Рік тому
You have a very comforting voice.
@dancedance6259
@dancedance6259 3 роки тому
Very good explanation! Good job and keep up the great work!!!
@cajhdz
@cajhdz 3 роки тому
Thank you, great example 💪🏻
@dylanhabedank8184
@dylanhabedank8184 Рік тому
Wow. this was amazingly explained. and easily followed. well done!
@user-tz5bz4cz9q
@user-tz5bz4cz9q Рік тому
Thanks! Great video and channel!
@sagivalia5041
@sagivalia5041 Рік тому
I find function pointers extremely useful in the case of implementing some kind of a shell or command line with a dictionary/map data structure where a string(command) corresponds to a function executing the command. I implement something like this in C# for a college project, at that point we weren't taught delegates(C#'s function pointers) and were expected to do it with switch cases, since I was too lazy I implemented a shell with a dictionary of commands which corresponds to it's delegate.
@CodeVault
@CodeVault Рік тому
Yes, function pointers are useful when using the functional programming paradigm. In other languages this is very well integrated (for example in JavaScript) or represent the code itself (for example in LISP). I suggest you look into functional programming, even in imperative languages it can become quite a useful tool that saves many lines of code for little performance penalty and makes for a much legible codebase
@aitbelhajayoub5400
@aitbelhajayoub5400 2 роки тому
i like your videos its easy to understand
@user-rk9zx3lt8k
@user-rk9zx3lt8k 8 місяців тому
thanks a lot! you are my best c coach, I just understand in C++, if transfer a function, we dont really need to use &fun_name. but in c, we have to
@CodeVault
@CodeVault 8 місяців тому
You don't have to do that in C either. It's optional. But I feel it's more straightforward and easier to understand that what you have there is a function reference
@plumtreegarden8352
@plumtreegarden8352 2 роки тому
Nice vid… I feel like you might be able to accomplish this even easier with a template
@CodeVault
@CodeVault 2 роки тому
Yep, for sure. What C lacks, C++ makes up for it in spades I think
@talroz8431
@talroz8431 3 роки тому
nice tutorial! whats about sockets in c?
@CodeVault
@CodeVault 3 роки тому
They are on the todo list
@parampampam9297
@parampampam9297 Рік тому
at 3:45 , why you use & in function call and * in function definition, can it be interchanged? I heard that I should use & in arguments of function definition as often as possible.
@CodeVault
@CodeVault 11 місяців тому
& in arguments is something from C++ and does not exist in C. It's called a reference and it's similar to what you have to do in C: With references (in C++): void a(int &x) { return x + 1; } int main() { int c; a(c); } With pointers (in C): void a(int* x) { return (*x) + 1; } int main() { int c; a(&c); } The biggest difference is you can overwrite that pointer x in the C implementation and force it to point to some other place in memory
@bsgamer5069
@bsgamer5069 3 роки тому
Owooo. This is awesome.
@user-sl4th2pu1z
@user-sl4th2pu1z 2 роки тому
Imagine i have an class with set of foo's: foo1(), foo2(), foo3(), foo4(),...,fooN(). And i also have a member function "execute_foo(int n)" that takes a number as a parameter and executes the related to this number fooN(). I know how to implement it with switch-case construction. But i will get suffer writing a 100 "case" statements. Is it possible to do it without huge code?
@CodeVault
@CodeVault 2 роки тому
Classes are only available in C++ ... I assume that's what you're using. You could look into C++ templates, they are a very powerful tool which could help in this case: www.cplusplus.com/doc/oldtutorial/templates/
@Dulge
@Dulge Рік тому
I understand it but why use pointers over references and maybe could you show why pointers are used in the windows api (Win32 api), thanks
@CodeVault
@CodeVault Рік тому
"References" as in variables with the & character before it? Those are part of C++ and not C, all my videos are working with C. I don't have much experience with the win32 API but I might do some small tutorials for it
@Dulge
@Dulge Рік тому
@@CodeVault oh i see, thanks
@hugo-garcia
@hugo-garcia 3 роки тому
Can you save encrypted txt files in C ?
@CodeVault
@CodeVault 3 роки тому
Yeah, although it's not too easy. Here's an example: www.example-code.com/C/crypt_aes_encrypt_file.asp
@fernando7517
@fernando7517 2 роки тому
🙌🙌👏👏
@pattyspanker8955
@pattyspanker8955 6 місяців тому
So, a hack-ish way of making functions first class in C?
@mechxware
@mechxware 10 місяців тому
sound of keyboard is very bad please reduce it ! you are great in speed writing but the sound is noisy
@CodeVault
@CodeVault 10 місяців тому
I'll see what I can do about it... Maybe it's time for a microphone upgrade
@Vutsman1
@Vutsman1 2 роки тому
Too fast, blew my mind. Error! Error!
@maxim25o2
@maxim25o2 2 роки тому
What You try to say, its that I can pas function to in side of another function? Or pass multiple functions with similar structure? Is this not bit strange and over complicated? I don't see programs and peoples using that at all. Is not easier to put function in side of another function from global scope anyway? I fill that this is cheating, like some functions existing in library where was created in local scope, and where normally programmist don't have access to it, then function pointer can get access to it?? This is cheating and in my opinion this is braking rools and integrity of the program and plans of previous programmer. Is'n it? I want also mention about lambdas, Lambda make is similar to what You explain, and I think in C++ lambda should be used. And pointers to function are made in C language.
@CodeVault
@CodeVault 2 роки тому
In functional programming we pass functions as parameters all the time and it's much more intuitive. But yes, it could get complicated if you have too many and too complex functions. And of course, in C++ you should probably use lambdas.
@wiktorwektor123
@wiktorwektor123 2 роки тому
Function pointers existed long before lambdas. If anything is "cheating" it's lambdas. And this isn't about C++, it's about C, look at file name "main.c". Besides function pointers are very useful as callback functions is many GUI applications.
@grimvian
@grimvian 11 місяців тому
I think this great video should have been marked as advanced C. If a beginner saw this code, he or she would be intimidated. While I was learning about pointers, I heard about function pointers. Initially, I thought, 'Please stop now,' but they are useful. Actually, pointers are not that difficult for me; it's the syntax that is challenging. The syntax for function pointers is ugly, but it makes sense when explained.
@mrjlad6625
@mrjlad6625 8 місяців тому
this is way too fast
@CodeVault
@CodeVault 8 місяців тому
I believe you can change the speed of the video with SHIFT + , or SHIFT + . Hopefully it helps
@ichigo_nyanko
@ichigo_nyanko 2 роки тому
Just so you know, you are pronouncing 'integer' wrong. the g is pronounced like j in juice. not like g in god.
@CodeVault
@CodeVault 2 роки тому
Thanks for pointing it out! I got this feedback quite a few times and will try to fix my pronunciation in the future videos :D
@mazinmustafa9869
@mazinmustafa9869 2 роки тому
How about this method? It is a little bit cleaner #include int prod(int a, int b){return a*b;} int add(int a, int b){return a+b;} void printResults(int a, int b, int func(int, int)){printf("%d operator %d = %d ", a, b, func(a, b));} int main() { printResults(4, 3, add); printResults(4, 3, prod); return 0; }
Calling fork multiple times (part 2) (With a practical example)
24:36
Master Pointers in C:  10X Your C Coding!
14:12
Dave's Garage
Переглядів 261 тис.
skibidi toilet 73 (part 2)
04:15
DaFuq!?Boom!
Переглядів 31 млн
Не пей газировку у мамы в машине
00:28
Даша Боровик
Переглядів 1,7 млн
Essentials: Pointer Power! - Computerphile
20:00
Computerphile
Переглядів 460 тис.
Function pointers and callbacks
15:19
mycodeschool
Переглядів 330 тис.
are "smart pointers" actually smart?
9:44
Low Level Learning
Переглядів 67 тис.
Arrays as function parameters in C
13:28
CodeVault
Переглядів 12 тис.
why do header files even exist?
10:53
Low Level Learning
Переглядів 336 тис.
Why Function Pointers are Awesome
11:11
Jacob Beningo
Переглядів 4,4 тис.
Go Pointers: When & How To Use Them Efficiently
14:09
Anthony GG
Переглядів 61 тис.
why do void* pointers even exist?
8:17
Low Level Learning
Переглядів 306 тис.
POINTERS in C++
16:59
The Cherno
Переглядів 1 млн
The What, How, and Why of Void Pointers in C and C++?
13:12
Jacob Sorber
Переглядів 49 тис.
skibidi toilet 73 (part 2)
04:15
DaFuq!?Boom!
Переглядів 31 млн