Union Data Types | C Programming Tutorial

แชร์
ฝัง
  • เผยแพร่เมื่อ 29 ธ.ค. 2024

ความคิดเห็น • 29

  • @ebrimagajaga4639
    @ebrimagajaga4639 10 หลายเดือนก่อน +5

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

    • @PortfolioCourses
      @PortfolioCourses  10 หลายเดือนก่อน +1

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

  • @GaryChike
    @GaryChike 9 หลายเดือนก่อน +6

    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.

  • @jakobfredriksson2272
    @jakobfredriksson2272 ปีที่แล้ว +5

    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 :)

  • @yigitcoban9823
    @yigitcoban9823 ปีที่แล้ว +5

    I am so happy to learn this.

    • @PortfolioCourses
      @PortfolioCourses  ปีที่แล้ว

      That's awesome, I'm glad to hear that Yiğit! :-)

  • @calebkrauter4027
    @calebkrauter4027 11 หลายเดือนก่อน

    This was so helpful! Thank you.

  • @ramakrishna4092
    @ramakrishna4092 2 ปีที่แล้ว +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  2 ปีที่แล้ว +4

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

  • @hanaksi
    @hanaksi 2 ปีที่แล้ว +5

    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  2 ปีที่แล้ว +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 2 ปีที่แล้ว

      @@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 2 ปีที่แล้ว

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

    • @PortfolioCourses
      @PortfolioCourses  2 ปีที่แล้ว

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

  • @princedeka6054
    @princedeka6054 5 วันที่ผ่านมา

    good video bro

  • @ForeverNils
    @ForeverNils 9 หลายเดือนก่อน

    thumbs up for Xcode )

  • @mongraal2272
    @mongraal2272 3 ปีที่แล้ว +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. :-)

  • @salmantechnologies282
    @salmantechnologies282 2 ปีที่แล้ว

    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  2 ปีที่แล้ว +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 2 ปีที่แล้ว

      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  2 ปีที่แล้ว

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

  • @DemoboyOot
    @DemoboyOot ปีที่แล้ว

    why dodge talking about talking about polymorphism?

    • @PortfolioCourses
      @PortfolioCourses  ปีที่แล้ว +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.

    • @hearthat1334
      @hearthat1334 2 หลายเดือนก่อน

      You don't have to hide it. Me too I was really afraid to talk about polymorphism at the family dinner. It was always taboo in my family and in my neighborhood in general