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!
If I have a question about coding in C that is too involved for the Google AI search result, I come here. This channel is the best resource I have found to learn the complexities of C.
Hey CodeVault, thank you for giving me back my sanity. You and portfolio courses and so many lucid people online really serve as a nice place to refresh my mind after taking an online course for a certificate. Thanks a lot. I need this tutorial. Stay frosty!
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.
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.
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
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.
& 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
There's a playlist related to pointers on the website: code-vault.net/course/l73yvitkit:1610029044540 I might make a more general video about pointers as it's one of the most confusing concepts to beginners
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
"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
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?
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/
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.
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.
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.
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.
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; }
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!
If I have a question about coding in C that is too involved for the Google AI search result, I come here. This channel is the best resource I have found to learn the complexities of C.
Hey CodeVault, thank you for giving me back my sanity. You and portfolio courses and so many lucid people online really serve as a nice place to refresh my mind after taking an online course for a certificate. Thanks a lot. I need this tutorial. Stay frosty!
We needed this for one of our tasks today, you saved our bums :D
we really did need this our book didn't explain anything
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.
The way you type is so satisfying!
You have a very comforting voice.
Not many people talk about this. Function pointers are exciting! God bless you for doing this!!
Bravo, frate. Super tutorial. Felicitări !
Wow. this was amazingly explained. and easily followed. well done!
that was really useful, thanks gentleman
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.
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
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.
& 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
Very good explanation! Good job and keep up the great work!!!
Helpful, thanks
It's is the best video which gave me more clarity about pointers, can I know little more about it?
There's a playlist related to pointers on the website: code-vault.net/course/l73yvitkit:1610029044540
I might make a more general video about pointers as it's one of the most confusing concepts to beginners
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
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
Thanks! Great video and channel!
Thank you, great example 💪🏻
i like your videos its easy to understand
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
"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
@@CodeVault oh i see, thanks
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?
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/
nice tutorial!
whats about sockets in c?
They are on the todo list
Can you save encrypted txt files in C ?
Yeah, although it's not too easy. Here's an example: www.example-code.com/C/crypt_aes_encrypt_file.asp
So, a hack-ish way of making functions first class in C?
sound of keyboard is very bad please reduce it ! you are great in speed writing but the sound is noisy
I'll see what I can do about it... Maybe it's time for a microphone upgrade
Owooo. This is awesome.
🙌🙌👏👏
Too fast, blew my mind. Error! Error!
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.
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.
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.
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.
Just so you know, you are pronouncing 'integer' wrong. the g is pronounced like j in juice. not like g in god.
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
this is way too fast
I believe you can change the speed of the video with SHIFT + , or SHIFT + .
Hopefully it helps
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;
}