Advanced C #6: Lookup Tables

Sdílet
Vložit
  • čas přidán 12. 12. 2023
  • Small and efficient hash map like behavior in a small array.
    Social links:
    Website: cacharle.xyz
    Github: github.com/cacharle
    Linkedin: / charles-cabergs-328aa8214

Komentáře • 15

  • @razvbir
    @razvbir Před 5 měsíci +9

    They are called Designated Initializers, I knew about them. But this is the first time I've seen an actual use case. Thank you.

    • @cacharle
      @cacharle  Před 5 měsíci +1

      Thank you, I didn't know the name for those.

  • @ElPikacupacabra
    @ElPikacupacabra Před 2 měsíci +2

    The basic usecase for designated initialisers is when you want to initialise a very large but sparse array. That was already something very useful to me. This is just icing on the cake.

  • @starc0w
    @starc0w Před 4 měsíci

    I love this series, really great!
    2:45
    In fact, the subscript operator [ ] requires the type int (not unsigned int - although this is automatically converted).
    A negative number is allowed as long as it is ensured that the memory access behind the index 0 is allowed.
    x = ptr[-3]; // O. K.
    A character constant, e.g. 'A', is also of type int in C - and not of type char, as is often wrongly assumed. This is different from C++, where a character constant is of type char.
    Which would also fit your examples:
    You can also apply this idea to an array of char pointers
    enum
    {
    DENNIS = 0,
    BRIAN,
    KEN,
    CHARLES
    };
    char const * const names[] =
    {
    "Dennis Ritchie",
    "Brian Kernighan",
    "Ken Thompson",
    "Charles Cabergs"
    };
    ...
    printf("
    %s", names[CHARLES]);
    Cheers, starcow

    • @cacharle
      @cacharle  Před 4 měsíci +1

      Thank you for the thorough comment 😄
      The fact that negative numbers are allowed seems quite unintuitive but I'm guessing legacy is to blame.
      As for the character literal, I'm very surprised they are 4 bytes.. I thought you had to prefix them with L'A' for them to be 4 bytes long but that is indeed only in C++.
      Definitely would have been a pitfall I was going to run into at some point 😅

  • @jukkasarasti1819
    @jukkasarasti1819 Před 5 měsíci +5

    I thought it was a video about c# because of the title :D

    • @cacharle
      @cacharle  Před 5 měsíci +5

      Did I manage to convert you to the dark side though?

  • @XavierChanth
    @XavierChanth Před 2 měsíci +1

    Currently porting a project to C, I used this feature to convert some enums for which I need a string value to map to.

  • @cooltrashgamer
    @cooltrashgamer Před 4 měsíci

    That syntax is pretty new to me, so are we defining indexes to arrays as numerical ascii values, and then stored in the array are structs, which contain a function pointer and an int? How does all that work? It looks really cool, do you have a link to a github repo I could look at?

    • @cacharle
      @cacharle  Před 4 měsíci +1

      yes, it's just a syntax to have the index in any order you want, I think the index can be anything that can be converted to an unsigned int (ascii, int, enum are all fine)
      I guess you're referring to my sed project's parsing.
      Here is the lookup table with the struct and function pointers: github.com/cacharle/sed/blob/590c0290b0b54554b3fad663c23c3f9f630f8e9a/src/parse.c#L348C3-L348C3
      Here is the lookup table for escape characters: github.com/cacharle/sed/blob/590c0290b0b54554b3fad663c23c3f9f630f8e9a/src/parse.c#L67

    • @cooltrashgamer
      @cooltrashgamer Před 4 měsíci

      Thank you! I don't think those links work though :( @@cacharle

    • @cacharle
      @cacharle  Před 4 měsíci

      @@cooltrashgamer ah yes, forgot to make the repository public 😅, they should work now

    • @cooltrashgamer
      @cooltrashgamer Před 4 měsíci

      Awesome, thank you so much 🙏 @@cacharle

  • @EDEN-ex7wh
    @EDEN-ex7wh Před 5 měsíci

    Awesome!