Why are function pointers useful?

Поділитися
Вставка
  • Опубліковано 29 вер 2024

КОМЕНТАРІ • 55

  • @lag2170
    @lag2170 3 роки тому +8

    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

  • @moha_kunX
    @moha_kunX Місяць тому

    that was really useful, thanks gentleman

  • @datenshiddd
    @datenshiddd 2 роки тому +13

    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!

  • @robfei-u6b
    @robfei-u6b Рік тому

    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  Рік тому

      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

  • @cajhdz
    @cajhdz 3 роки тому

    Thank you, great example 💪🏻

  • @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

  • @dnorris654
    @dnorris654 День тому

    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.

  • @aldekorea
    @aldekorea Рік тому +2

    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.

  • @pattyspanker8955
    @pattyspanker8955 11 місяців тому

    So, a hack-ish way of making functions first class in C?

  • @mohdfarhanmulla4734
    @mohdfarhanmulla4734 4 місяці тому

    It's is the best video which gave me more clarity about pointers, can I know little more about it?

    • @CodeVault
      @CodeVault  4 місяці тому

      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

  • @mechxware
    @mechxware Рік тому

    sound of keyboard is very bad please reduce it ! you are great in speed writing but the sound is noisy

    • @CodeVault
      @CodeVault  Рік тому +1

      I'll see what I can do about it... Maybe it's time for a microphone upgrade

  • @Radiant_Cascade
    @Radiant_Cascade 22 дні тому

    Helpful, thanks

  • @gregtasi
    @gregtasi 2 роки тому +1

    The way you type is so satisfying!

  • @Vutsman1
    @Vutsman1 2 роки тому +1

    Too fast, blew my mind. Error! Error!

  • @sagivalia5041
    @sagivalia5041 2 роки тому +1

    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  2 роки тому

      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

  • @plumtreegarden8352
    @plumtreegarden8352 3 роки тому +1

    Nice vid… I feel like you might be able to accomplish this even easier with a template

    • @CodeVault
      @CodeVault  3 роки тому +3

      Yep, for sure. What C lacks, C++ makes up for it in spades I think

  • @alauda_projects
    @alauda_projects 3 місяці тому

    Bravo, frate. Super tutorial. Felicitări !

  • @davidliverman4742
    @davidliverman4742 2 роки тому

    Not many people talk about this. Function pointers are exciting! God bless you for doing this!!

  • @dylanhabedank8184
    @dylanhabedank8184 Рік тому

    Wow. this was amazingly explained. and easily followed. well done!

  • @pogchamper228
    @pogchamper228 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 роки тому +1

      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  Рік тому +1

      "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

  • @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  Рік тому

      & 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

  • @mrjlad6625
    @mrjlad6625 Рік тому

    this is way too fast

    • @CodeVault
      @CodeVault  Рік тому

      I believe you can change the speed of the video with SHIFT + , or SHIFT + .
      Hopefully it helps

  • @karimjerbi7084
    @karimjerbi7084 2 роки тому

    You have a very comforting voice.

  • @АндрейБорисов-и4о
    @АндрейБорисов-и4о 2 роки тому

    Thanks! Great video and channel!

  • @aitbelhajayoub5400
    @aitbelhajayoub5400 2 роки тому

    i like your videos its easy to understand

  • @maxim25o2
    @maxim25o2 3 роки тому

    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  3 роки тому

      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.

  • @dancedance6259
    @dancedance6259 3 роки тому

    Very good explanation! Good job and keep up the great work!!!

  • @bsgamer5069
    @bsgamer5069 3 роки тому

    Owooo. This is awesome.

  • @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

  • @mazinmustafa9869
    @mazinmustafa9869 3 роки тому

    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;
    }

  • @fernando7517
    @fernando7517 2 роки тому

    🙌🙌👏👏

  • @talroz8431
    @talroz8431 3 роки тому

    nice tutorial!
    whats about sockets in c?

    • @CodeVault
      @CodeVault  3 роки тому +5

      They are on the todo list

  • @grimvian
    @grimvian Рік тому

    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.