Union Data Types | C Programming Tutorial

Поделиться
HTML-код
  • Опубликовано: 31 дек 2021
  • How to use union data types in C. Source code: github.com/portfoliocourses/c.... Check out www.portfoliocourses.com to build a portfolio that will impress employers!
  • ХоббиХобби

Комментарии • 31

  • @ebrimagajaga4639
    @ebrimagajaga4639 3 месяца назад +4

    You have simplified everything for me..... I now know the difference between Unions and Structures in C

    • @PortfolioCourses
      @PortfolioCourses  3 месяца назад

      That's excellent, I'm glad the video helped you out! :-)

  • @jakobfredriksson2272
    @jakobfredriksson2272 8 месяцев назад +3

    Great lesson, as always (even if I'm late to the party)!
    It might be difficult for new programmers to come up with use cases where unions are useful "for real". I'd no idea myself before a senior programmer showed me how he utilized unions for parsing protocols of various kind. It blew my mind :)

  • @calebkrauter4027
    @calebkrauter4027 4 месяца назад

    This was so helpful! Thank you.

  • @GaryChike
    @GaryChike 2 месяца назад +2

    Excellent explanation! But perhaps there should be some mention of the concept of "most significant bytes" vs "least significant bytes". Some new learners may be surprised by the output, when they think that assigning one value will completely overwrite the previous value - which may not occur if the previous value is larger than the subsequent value. Such as in this example whereby the char type is assigned 127, but 'int a' is suddenly 383 instead of 127.
    #include
    typedef union MyUnion
    {
    int a;
    char b;
    }myUnion;
    int main(void)
    {
    myUnion abc;
    abc.a = 256;
    printf("%d %d
    ", abc.a, abc.b); // 256 0
    abc.b = 127;
    printf("%d %d
    ", abc.a, abc.b); // 383 127
    return 0;
    }
    Binary 256: 0000 0001 0000 0000
    Binary 127: 0111 1111.

  • @yigitcoban9823
    @yigitcoban9823 Год назад +5

    I am so happy to learn this.

  • @ramakrishna4092
    @ramakrishna4092 Год назад +4

    Hi sir good to see you back
    Can you explain what are anonymous unions and structs and why is it called anonymous...
    Thank you for understanding...

    • @PortfolioCourses
      @PortfolioCourses  Год назад +4

      That's a good idea for a video! :-) Hopefully one day I can make a video on anonymous structs and unions.

  • @ForeverNils
    @ForeverNils 2 месяца назад

    thumbs up for Xcode )

  • @mongraal2272
    @mongraal2272 2 года назад +4

    nice.

  • @justcurious1940
    @justcurious1940 Год назад

    What about these anonymous unions, are they special or just like any other nested union ?

    • @PortfolioCourses
      @PortfolioCourses  Год назад +1

      I've never had to use anonymous unions, maybe one day I'll research it and do a video on the topic. :-)

    • @justcurious1940
      @justcurious1940 Год назад

      ​@@PortfolioCourses Thank u ,make videos about sockets and spice them with some threads 🙂🙂

    • @PortfolioCourses
      @PortfolioCourses  Год назад

      Hopefully one day. :-)

  • @nicholas_sergakis
    @nicholas_sergakis Год назад

    👏👏

    • @PortfolioCourses
      @PortfolioCourses  Год назад +2

      Thank you Nicholas! :-D

    • @nicholas_sergakis
      @nicholas_sergakis Год назад

      @@PortfolioCourses Since I discovered you, I learn from your videos almost daily. I have a Data Structures course in college and till now, you've effectively explained to me Structs and Unions. Now I am watching the Dynamic Memory Allocation video. You are awesome :-)

    • @PortfolioCourses
      @PortfolioCourses  Год назад +2

      @@nicholas_sergakis Awesome!!! 🙂

  • @hanaksi
    @hanaksi Год назад +3

    6:43 why is that 48 bytes? We have a 32 characters array, a double and an int, 32 bytes + 8 bytes + 4 bytes = 44 bytes and we have a sizeof(mydata2) is 48 bytes. WHY?

    • @PortfolioCourses
      @PortfolioCourses  Год назад +4

      That's a really great question! :-) It has to do with what is called structure padding: www.edureka.co/blog/understanding-structures-and-padding-in-c/. I might make a video on that topic one day.

    • @hanaksi
      @hanaksi Год назад

      @@PortfolioCoursesThank you so much for your response Kevin! I already figured out what the idea is here by looking for answers on the internet

    • @hanaksi
      @hanaksi Год назад

      @@PortfolioCourses It will be really nice if you make a video on this topic!

    • @PortfolioCourses
      @PortfolioCourses  Год назад

      @@hanaksi I think I will it's a bit of a niche topic but interesting.

  • @salmantechnologies282
    @salmantechnologies282 Год назад

    const struct {uint32_t len; const char *data;} control_blocks[ ] = {
    {count_of(word0) - 1, word0}, // Skip null terminator
    {count_of(word1) - 1, word1},
    {count_of(word2) - 1, word2},
    {count_of(word3) - 1, word3},
    {count_of(word4) - 1, word4},
    {count_of(word5) - 1, word5},
    {0, NULL} // Null trigger to end chain.
    };
    Sir How this struct Works that's i got from pi pico data sheet basically i am doing embedded developing kindly help me with this ....

    • @PortfolioCourses
      @PortfolioCourses  Год назад +1

      It looks like the code will create an array of structs, where each struct has two members. The member len will hold what looks like the length of a word, and the member data will store a pointer to the word (which looks like its a string). the last struct stored in the array appears to be a special struct with len set to 0 and data set to NULL, it seems to signify the end of the data. That's about as much as I can help you hear though I think. :-)

    • @salmantechnologies282
      @salmantechnologies282 Год назад

      Thanks sir exactly what it is the structured 2d array first is length of array word count_of is a function return size of array and the data pointer which points the word array thanks for your help Sir i appreciate your help and i will share your channel on every social media to spread your knowledge and teaching style ❤

    • @PortfolioCourses
      @PortfolioCourses  Год назад

      @@salmantechnologies282 You're welcome, and thank you so much for helping to spread the word about this channel I appreciate that. 🙂

  • @DemoboyOot
    @DemoboyOot 10 месяцев назад

    why dodge talking about talking about polymorphism?

    • @PortfolioCourses
      @PortfolioCourses  10 месяцев назад +1

      Why do you think I "dodged" the topic? The top search results for unions in C are all educational resources that make no mention of polymorphism:
      www.tutorialspoint.com/cprogramming/c_unions.htm
      www.programiz.com/c-programming/c-unions
      www.geeksforgeeks.org/c-unions/
      So it's not unusual to not mention polymorphism when talking about unions. It's true that you can implement a low-level form of polymorphism with unions. I suppose if I ever cover that it would be another video, maybe as part of implementing polymorphism in C in general with other tools like stucts, etc.