C Programming Tutorial 41, Unions

Comments

TangoBravo says:

I like your intention with this video, but I think there are a couple problems with it–and I think people should realize these. First, at around 3:00, you show the memory layout of the integer value ‘4’ as 0 0 0 4. This is implying big endian, where the most significant bit (MSB) is stored at the lowest address (which is implied to be on the left). But then you show the single-byte char value as going into the *least* significant byte–at the right end. Wouldn’t it go into the left end?

iTzAdam5X says:

You use the -> operator to access stuff inside of a pointer to a struct/union. It doesn’t matter if the field you’re trying to access in the struct is a pointer or not, it matters if the struct itself is a pointer.

Nikolay Georgiev says:

In the report card problem you sait that whenever we have pointers we should use the arrow operator. Why do you still use the dot operator here when *firstname and *secondname are both pointers???

Anony mous says:

thanks, what forum or web page do you suggest me to see libraries descriptions.

Shajeda Ahmed says:

A UNION  B UNION  C UNION  D 

can this be done in c++

iTzAdam5X says:

You use the -> operator to access stuff inside of a pointer to a struct/union. It doesn’t matter if the field you’re trying to access in the struct is a pointer or not, it matters if the struct itself is a pointer.

1q1q1q1q1q1q1qw says:

union useless

newmagicfilms says:

Great tutorial! You are a really good teacher

Darshan Parajuli says:

@iTzAdam5X hehe u got me…..but i thought this vid was about union only…..so i only watch the beginning of it…. never mind XD

FLXStudios says:

Tom B got the answer… its to conserve memory for embedded code ^^s o you can reuse the same bytes over and over again for different things

Tom B says:

There’s not much point anymore, unless you’re trying to conserve memory…say in an embedded application. Keep in mind that the Union data structure was implemented in C many moons ago, when RAM was at a premium. The concept is to simply not use more memory than you need at any one time. So if you don’t need 100 bytes for a struct because you might only be using 20 of those bytes at any one time–then use a Union. You’ll get your 20 bytes, but the other 80 won’t go to waste because you…

Amibuebe says:

An explanation about what the point of a union is would’ve been good at the end. I understood what you said, I just can’t imagine the use for this. Great video though.

Tom B says:

No sweat. Unless you want to write embedded code (which I would advise against when learning to program), don’t worry about unions. Get yourself up-to-speed on the struct data type, and then of course on classes. Later on, after you’ve mastered that material, then come back to visit unions again. To tell you the truth, once you learn how to use structs well…you pretty much also know unions.

Good luck!

Anony mous says:

what is the point of unions? i dont understand D:

Anony mous says:

what is the point of unions? i dont understand D:

Tom B says:

…aren’t using them. But if you use a union instead, the compiler will figure out how much memory you absolutely need (for that data structure) and then ask the OS for only that much. You just have to reuse that chunk for the other members of the union, when you need to use them. In other words, you cannot use all 100 bytes at the same time–if you need to do that, then use the struct type. So it’s about conserving memory, which you might need to do for an embedded app where every byte counts.

TangoBravo says:

The other problem is as I’ve mentioned in one of your previous video threads: Your representation of the addresses stored by the pointers is incorrect. You’ve already covered Hex, so just use that–stick a “0x” in front of “1234” or “5678” and your representation will be just fine. Then the “12” (or “56”) is one byte of a two-byte address, and the “34” (or “78”) is the other byte. At least then you’d be accurate–because I guarantee that an integer is not laid out in memory the way you’ve shown.

Siniša Petrović says:

Where and when to use them? Sorry mate, but this tutorial is pointless…

Anony mous says:

thank you, it is a lil bit dificult for me to understand these themes, im a begginer and i am trying to study a whole book of c++, but it teaches me 1st C program so its on it, thanks for your answer.

nicholus dayile says:

what is the point if using structures and unions?

iTzAdam5X says:

You use the arrow operator to access stuff inside of a pointer to a struct/union. It doesn’t matter if the field you’re trying to access in the struct is a pointer or not, it matters if the struct itself is a pointer.

iTzAdam5X says:

@lovepuremetal5 Did you watch the video?

Write a comment

*

Human Verification: In order to verify that you are a human and not a spam bot, please enter the answer into the following box below based on the instructions contained in the graphic.


Do you like our videos?
Do you want to see more like that?

Please click below to support us on Facebook!

Send this to a friend

▷ Other ReviewsVehicles   Show Cars   Motorbikes   Scooters   Bicycles   Rims & Tires   Luxury BoatsFashion   Sunglasses   Luxury Watches   Luxury Purses   Jeans Wear   High Heels   Kinis Swimwear   Perfumes   Jewellery   Cosmetics   Shaving Helpers   Fashion Hats   Modeling TipsFooding   Chef Club   Fooding Helpers   Coktails & LiquorsSports   Sport Shoes   Fitness & Detox   Golf Gear   Racquets   Hiking & Trek Gear   Diving Equipment   Ski Gear   Snowboards   Surf Boards   Rollers & SkatesEntertainment   DIY Guides   Zik Instruments   Published Books   Music Albums   Cine Movies   Trading Helpers   Make Money   Fishing Equipment   Paintball Supplies   Trading Card Games   Telescopes   Knives   Vapes   GameplaysHigh Tech   Flat Screens   Tech Devices   Camera Lenses   Audio HiFi   Printers   USB Devices   PC Hardware   Network Gear   Cloud Servers   Software Helpers   Programmer Helpers   Mobile Apps   Hearing AidsHome   Home Furniture   Home Appliances   Tools Workshop   Beddings   Floor Layings   Barbecues   Aquarium Gear   Safe Boxes   Office Supplies   Security Locks   Cleaning ProductsKids   Baby Strollers   Child Car Seats   Remote ControlledTravel   Luggages & Bags   Airlines Seats   Hotel Rooms   Fun Trips   Cruise Ships   Mexico Tours