A union works like a structure with a similar declaration, members, and reference to the members. The difference between a union and structure is that the union's members all share the same data space. Unlike a structure, you can set and access only one member of a union at a time. The union's shared data space makes its implementation risky as it's possible to corrupt the shared storage space unintentionally.
- [Narrator] A union is one of those … funky C Language tools seldom used and rarely understood. … It's related to a structure, sort of like a structure … with an added level of flexibility, … but it comes with a warning. … First, let me demonstrate a type of union … which you see here, … declared in this coded line five. … The union is named storage, … and at line eight, a variable reg is created … of these storage union type. … Yes, it looks a lot like a structure, … but it's two members to find that lines six and seven … character a and integer b, … they share the same storage space. … They are not discrete values. … Now in the rest of the code, … character a is assigned the member reg.a, … which you see used at line 10, … and then at line 13, a value is assigned to member reg.b. … So far, the union looks like a structure. … In fact, if you changed union to structure at line five, … the code would still work, … but it's not a structure. … Build and run. … And you see the values of each member …
- Working with arrays
- Building a structure
- Creating an array of structures
- Testing characters
- Working with strings in C
- Using pointers to manipulate data
- Manipulating files
- Using command-line arguments
- Working with time functions
Skill Level Intermediate
1. Arrays and Structures
2. Characters and Strings
4. Files and the Operating System
5. The Interesting and Extraordinary
Next steps1m 52s
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.