C Programming Tutorial 41, Unions

preview_player
Показать описание

Рекомендации по теме
Комментарии
Автор

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.

iTzAdamX
Автор

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.

iTzAdamX
Автор

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.

amibuebe
Автор

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...

bluehornet
Автор

If you don't want any garbage in the union basically, you just develop the object outside the main in the global region.

PerchEagle
Автор

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.

tcbetka
Автор

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!

bluehornet
Автор

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.

anonymous__
Автор

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

anonymous__
Автор

Great tutorial! You are a really good teacher

newmagicfilms
Автор

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

FLXStudios
Автор

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???

nikolaygeorgiev
Автор

...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.

bluehornet
Автор

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?

tcbetka
Автор

@lovepuremetal5 Did you watch the video?

iTzAdamX
Автор

The way you are using char* without malloc, it's risking a segfault, no?

AkamiChannel
Автор

what is the point if using structures and unions?

neonik
Автор

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

anonymous__
Автор

A UNION  B UNION  C UNION  D 

can this be done in c++

shajedaahmed
Автор

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

DarshanParajuli
welcome to shbcf.ru