Title says it all: I am wondering in what kind of situations a programmer would use a union vs using a structure. I'm pretty new to this stuff and my book only devotes about 2 pages to Unions so it isn't the best source of info on Unions really, but they look like they could be used to save a lot of RAM and I would like to know what kind of programs or functions they are intended for. Thanks, James.
Jamblaster
15
Newbie Poster
Recommended Answers
Jump to PostSave lots of RAM? Not really.
Here's a union example to find the byte order of a system.
#include <iostream> int main(int argc, char**argv) { union { short s; char c[sizeof(short)]; } un; un.s = 0x0102; if (sizeof(short) == 2) { if ( un.c[0] == 1 …
Jump to PostUnions is a poor man's way to express the idea of polymorphism. See for example a definition of X Windows event and related API.
All 6 Replies
gerard4143
371
Nearly a Posting Maven
nezachem
616
Practically a Posting Shark
gerard4143
371
Nearly a Posting Maven
Narue
5,707
Bad Cop
Team Colleague
nezachem
616
Practically a Posting Shark
rubberman
1,355
Nearly a Posting Virtuoso
Featured Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.