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?
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.
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???
thanks, what forum or web page do you suggest me to see libraries descriptions.
A UNION B UNION C UNION D
can this be done in c++
Great tutorial! You are a really good teacher
@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
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
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…
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.
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.
what is the point of unions? i dont understand D:
…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.
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.
Where and when to use them? Sorry mate, but this tutorial is pointless…
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.
what is the point if using structures and unions?
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.
@lovepuremetal5 Did you watch the video?