Application of Unions (Part 1)

Поделиться
HTML-код
  • Опубликовано: 5 окт 2019
  • C Programming: Application of Unions in C Programming.
    Topics discussed:
    1) Accessing the members of the structure.
    2) Printing the size of the structure.
    C Programming Lectures: goo.gl/7Eh2SS
    Follow Neso Academy on Instagram: @nesoacademy(bit.ly/2XP63OE)
    Follow me on Instagram: @jaspreetedu(bit.ly/2YX26E5)
    Contribute: www.nesoacademy.org/donate
    Memberships: bit.ly/2U7YSPI
    Books: www.nesoacademy.org/recommende...
    Website ► www.nesoacademy.org/
    Forum ► forum.nesoacademy.org/
    Facebook ► goo.gl/Nt0PmB
    Twitter ► / nesoacademy
    Music:
    Axol x Alex Skrindo - You [NCS Release]
    #CProgrammingByNeso #CProgramming #UnionsInC #UnionApplications #Structure&Union

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

  • @suheabkhan2546
    @suheabkhan2546 4 года назад +41

    Sir.. Missing ur lectures.. .. Huge fan of ur teaching

    • @suheabkhan2546
      @suheabkhan2546 4 года назад +1

      Sir when us data structures is going to start

    • @westridgelearnings7744
      @westridgelearnings7744 2 года назад +1

      how to access price of an item in above structure... s.item.price=9? or s.price=9? explain
      @Neso Academy

    • @jarraf
      @jarraf 11 месяцев назад

      s.price

  • @amansaxena9794
    @amansaxena9794 4 года назад +11

    Thank u sir u cleared my all doubt related to union

  • @DaminGamerMC
    @DaminGamerMC 3 года назад +2

    Awesome tutorial, you go to the point and explain clearly what unions do which is what i wanted to know. Thaks!

  • @prakashkrsingh0
    @prakashkrsingh0 4 года назад +1

    Wonderful... Thank you so much.. Happy दशहरा ..

  • @067_prahladmondal7
    @067_prahladmondal7 2 года назад

    Ur classes are just awesome!

  • @abheejitjadhav9405
    @abheejitjadhav9405 4 года назад +1

    Very clearly explained....thanks

  • @supersakib62
    @supersakib62 2 года назад

    Excellent to the point explanation.

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

    Here is a big fan from Turkey, thanks for your videos

  • @dipeshchandiramani1897
    @dipeshchandiramani1897 3 года назад +2

    Very helpful!

  • @kunalsoni7681
    @kunalsoni7681 4 года назад +1

    nice one presentation sir i love this too much because your explanation is awesome

  • @hadireg
    @hadireg 3 года назад

    great example here! :) Thanks!

  • @yesodabhargava3904
    @yesodabhargava3904 2 года назад

    This is great. Thank you so much 🙂

  • @josebatista6259
    @josebatista6259 2 года назад

    The alchemist is a great book, Sir! claps from Brazil

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

    Neso>>>>every other utube lecturer 🎉

  • @purubhargava8096
    @purubhargava8096 3 года назад

    loved it sir ji

  • @AmarKumar-vo2bv
    @AmarKumar-vo2bv 4 года назад +1

    Thank u very much for the video. Sir please also make video on union inside structure and structure inside union...

  • @saddamahmad2310
    @saddamahmad2310 4 года назад

    thank you very much sir for this video

  • @mcbotface
    @mcbotface 4 года назад +3

    Finally NESO Academy listened !!

  • @user-bu8mg7uq3s
    @user-bu8mg7uq3s 3 года назад +1

    thank you

  • @AnkitSharma-oq4wi
    @AnkitSharma-oq4wi 4 года назад +1

    Sir please keep uploading regularly.Its kind of a habit now

  • @AlokSingh-cb3fv
    @AlokSingh-cb3fv 4 года назад

    Thank you

  • @abhineelnandi9552
    @abhineelnandi9552 3 года назад +1

    Love from Antartica ❤❤❤❤

  • @Nervenkugel
    @Nervenkugel 2 года назад +1

    but if they share the same memory location modifying the values of book swill affect the values of shirt. right ?

  • @narendrajayram1317
    @narendrajayram1317 3 года назад +5

    Two doubts:
    1. In the example mentioned , how do you differentiate between book's price and shirt's price? In other words, why didn't you place the 'price' member within structures inside union?
    2. In union, members will share the same memory location. That means, if we make changes to one member, it will be reflected on the other member as well, isn't it? How come it is not happening here?
    Is creating a separate variable for book is a solution for doubt 1?
    struct store b;

    • @pradhanan9413
      @pradhanan9413 2 года назад +3

      Answering to your 2nd doubt,
      Yes it will be reflected in the other member of the union.
      And that's why sir told to use either book structure or t shirt structure. We can't use both.

  • @hellloooworld1124
    @hellloooworld1124 3 года назад

    Luv it

  • @DrOggy67
    @DrOggy67 3 года назад +1

    Wouldn't it be helpfull to define an additional variable in struct store to distinguish between book and shirt? Otherwise you don't know, what kind of information will be found within the union.

  • @yesodabhargava3904
    @yesodabhargava3904 2 года назад

    In the video for code at 4:35, I could not understand why there was no name after union or struct ; why directly { started. Could you please help me out with this one? Thank you.

  • @harcharansingh1997
    @harcharansingh1997 3 года назад +1

    In the main function there is no use of color ,size and design then why there is need of enter structure varible of these just for the purpose of memory saving????

  • @HeraldOD
    @HeraldOD 3 года назад

    Nice book tastes :)

  • @ashishsingh7056
    @ashishsingh7056 4 года назад +1

    huge fan sir please make an leacture fo python😀😀😀😀🙂🙂🙂

  • @TheAnkitNainwal
    @TheAnkitNainwal 3 года назад

    best book alchemist

  • @accomplishdreams1404
    @accomplishdreams1404 8 месяцев назад

    Does union also have the concept of padding?

  • @MKSundaram
    @MKSundaram 3 года назад

    How could I initialize the price of the book/shirt and access it?

  • @adeeptamitramajumder2424
    @adeeptamitramajumder2424 4 года назад +3

    You said union members share same memory location and change in one gets reflected in others members too. Then "book" and "shirt" structures are two members of the Union. If we set values in book members then why is it not reflected in shirt members?

    • @badxcode
      @badxcode 4 года назад +3

      it reflected in my code tho. I din change the data type in printf() and showed same result with 0 errors.
      I put the same data type as book just changed the name book to "shirt"
      like::
      printf("%s", s.item.book.title); and printf("%s", s.item.shirt.color); showed the same output with 0 errors.
      Even though s.item.book.title = string literal and s.item.shirt.color = int data type
      When I used both with "%s" they showed the same output. I don't know why.
      I had the same question as yours.

    • @nbk330
      @nbk330 3 года назад

      @@badxcode same here..!!

  • @thinq5339
    @thinq5339 2 года назад

    Sir if we add all the members to a single union what is the problem. Then the total size will be 8 byte.

  • @AmarKumar-vo2bv
    @AmarKumar-vo2bv 4 года назад

    Which situation we use union inside structure

  • @turtlepedia5149
    @turtlepedia5149 4 года назад

    Sir plz upload data structures in c programming

  • @twinklekumari3981
    @twinklekumari3981 3 года назад

    Can anyone please explain why the struct declared under union has no name associated with it. Was it a mistake???

  • @AnandaLewis-256
    @AnandaLewis-256 4 месяца назад

    What does the # pragma pack(1) mean

  • @westridgelearnings7744
    @westridgelearnings7744 2 года назад

    how to access price of an item in above structure... s.item.price=9? or s.price=9? explain
    @Neso Academy

  • @zerobit778
    @zerobit778 3 года назад +4

    Great video.
    And why don't we just separate store to two structs like struct book and struct shirt?

    • @MrChrisiscool15
      @MrChrisiscool15 2 года назад

      because when creating two different structs and you include the member "price" in both structs waste space.

  • @sivashankaribadrinarayanan1272
    @sivashankaribadrinarayanan1272 3 года назад

    y we using pointer to specific member title author and design

  • @Yash_Raj24
    @Yash_Raj24 3 года назад +1

    There's a question ..how will you differentiate between the price of book and the price of shirt?

    • @aquapisces
      @aquapisces 3 года назад +1

      s.item.?.price= x
      So in place of ? Whatever u insert is the price

  • @saikrishnathota5972
    @saikrishnathota5972 4 года назад

    Upload network lectures sir

  • @ssc_-kn6os
    @ssc_-kn6os 3 года назад

    Naming of structures book and shirt are missing in the last application 5:20

  • @manishsharma1298
    @manishsharma1298 4 года назад

    how to add price ??

  • @chingkhei_thoudam
    @chingkhei_thoudam 2 года назад

    Why the character are declared as pointer but not array?

  • @anikethdanam2391
    @anikethdanam2391 2 года назад

    if we remove structs inside unions and include 2 unions, we get size as 16

  • @beeyou6220
    @beeyou6220 2 года назад

    why char ptr is holding 8 bytes??

  • @prateekkumar4728
    @prateekkumar4728 2 года назад

    How first structure has size 20 bytes

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

    For my 64 bit processor pointer (char *, int * , double * ) all taking 4 bytes

  • @lalit2926
    @lalit2926 4 года назад +1

    What if I want to store the information of book and shirt at the same time sir??

  • @mcbotface
    @mcbotface 4 года назад

    What if I want to also store a shirt information and want to display both book information and shirt information at the same time ? Can I do it ?

  • @addagallanaresh4044
    @addagallanaresh4044 4 года назад

    pointe size is 8 bytes but title takes 12 bytes of memory how it is managed

  • @aditisingh4952
    @aditisingh4952 2 года назад

    Something is wrong with the code, i cannot run it! Pls someone guide me

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

    fell in love with your channel, but why always the char variable has to be pointers? why every char variable has the * before it?!

    • @pawankapure7593
      @pawankapure7593 4 года назад +1

      As we have to store string value like author name , we need need to declare char pointer which represents first memory location of our string char

    • @qandos-nour
      @qandos-nour 4 года назад +1

      it is called "String literal" you can watch neso vedios, it is simply a way to store strings

  • @bholekadeewana4628
    @bholekadeewana4628 2 года назад

    how char is taking 8 bytes instead of 1 bytes

  • @venkateshpolisetty5624
    @venkateshpolisetty5624 4 года назад +1

    How can COLOR be an integer type?

    • @harcharansingh1997
      @harcharansingh1997 3 года назад +1

      Just consdering for your understanding on concept of unions

    • @harcharansingh1997
      @harcharansingh1997 3 года назад

      Just consdering for your understanding of unions

  • @qandos-nour
    @qandos-nour 4 года назад

    it seems to me that this program not perfect and has lots of problems
    personally I had problems in understanding it :(
    I could not add two books or shirts!
    how could I create variable inside main not in the structure definition.

  • @juhairmanar7055
    @juhairmanar7055 2 года назад

    But my compiler showing the size 32bytes.

  • @CB-yl7uc
    @CB-yl7uc 4 года назад

    Sir if we use the shirt properties
    Then what will be the size of union??

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

      The size of the union will *always* be that of its largest member, even if you use the smaller member. If we, as in your question, use the shirt, it will still use the size of the book (but wont use it all).

    • @CB-yl7uc
      @CB-yl7uc 4 года назад

      @@lionkor98 thnxxx 🙏🙏🙏

    • @pradhanan9413
      @pradhanan9413 2 года назад +1

      @@lionkor98 if we, assign values to the variables of t shirt structure, then won't the values of the variables of book structure get affected? Because these structures are stored inside union right.....so they have same memory allocation and varying one member affects the other.
      Am I correct?

  • @robinsharma7920
    @robinsharma7920 2 года назад

    Why is this code giving error in C++

  • @ahmedrahmane9972
    @ahmedrahmane9972 4 года назад

    C or C++?

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

    sir is that possible to use union 3:17 here

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

      instead of struct

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

      //like this
      #include
      union store{
      double price;
      char *title;
      char *author;
      int num_page;
      int color;
      int size;
      char *design;
      };
      int main()
      {
      union store book;
      printf("%ld bytes
      ",sizeof(book));
      return 0;
      }

  • @ritamchatterjee3307
    @ritamchatterjee3307 3 года назад

    how color can be int, and why every character is a pointer
    explain anyone
    pleaseeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee

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

    Only curious person search this topic

  • @ashishsingh7056
    @ashishsingh7056 4 года назад

    char * takes 8 byte i don't understand please help me

    • @Manneedi
      @Manneedi 3 года назад

      any pointer pointing to astring literal takes 8 bytes of memory

  • @anikethdanam2391
    @anikethdanam2391 2 года назад

    // WE GET SIZE AS 16, IF WE remove structs inside unions and include 2 unions
    #include
    #include
    struct store{
    union{
    char *title;
    char *author;
    int pages;
    }book;
    union{
    char *color;
    int size;
    char *design;
    }shirt;
    };
    int main(){
    struct store s;
    s.book.title = "Naruto";
    s.shirt.design = "Orange";
    printf("%s
    ",s.book.title);
    printf("%s
    ",s.shirt.design);
    printf("%ld",sizeof(s));
    return 0;
    }

  • @SnehithaCse
    @SnehithaCse 4 года назад

    Char occupies 1 byte right then it's given 8 bytes why so 🙄

    • @qandos-nour
      @qandos-nour 4 года назад +2

      it is not a char, it is pointer to char, string literal

    • @nihalpandey5675
      @nihalpandey5675 4 года назад

      Hey snehita are you from bhopal 🧐

  • @user-dr6cd1qd6y
    @user-dr6cd1qd6y 7 месяцев назад

    wouldn't this mean that when we enter the data for the book structure the shirt structure will also be initialized with the same value. let's say we wanted struct 1 to be about fiction and struct 2 to be about non-fiction. when we initialize the fiction with author and title and price wouldn't the non-fiction become initialized with false data that doesn't belong to it?

  • @ivanleon6164
    @ivanleon6164 3 года назад

    E Musk and J. Bezos: Unions? we don't do that here, not accepted in interviews.

  • @thinhnguyenvan7003
    @thinhnguyenvan7003 3 года назад +1

    I think, this case study is useless. We can create two struct book and shirt . Right?

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

    depois de Paulo Coelho eu parei de prestar atenção kkkkkkkkk

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

    Teach this
    to theGoogle Chrome Programmers 😂