473,386 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Application of Union in C

I want what is the application of union in C.Please explain

Apr 19 '06 #1
2 7152
On 2006-04-19, balasam <bk*****@gmail.com> wrote:
I want what is the application of union in C.Please explain


They're quite often used for a sort of "polymorphism".

For example, if you were writing an interpreter for a dynamically typed
language, you might represent values in the language using unions like
this:

enum value_type
{
INT,
REAL,
STRING,
OBJECT
};

struct value
{
enum value_type type;

union
{
int val_int;
double val_real;
char *val_string;
struct object *val_object;
}
value;
};

All values are instances of "struct value", but that can represent an
int, a double, a char or an object.
Apr 19 '06 #2
CJ
A common use for union is to ease breaking up a STREAM of input, in the
example below breaking an int value read into two short values.

/* tunion.c */
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

/* Usage: tunion <binfile */
int
main(int argc,char *argv[])
{
union {
unsigned ii;
struct {
short high;
short low;
} word;
} decode;

printf("sizeof(decode) = %d\n",sizeof(decode));
printf("sizeof(decode.ii) = %d\n",sizeof(decode.ii));
printf("sizeof(decode.word) = %d\n",sizeof(decode.word));
printf("sizeof(decode.word.high) =
%d\n",sizeof(decode.word.high));
printf("sizeof(decode.word.low) =
%d\n",sizeof(decode.word.low));

/* Reads 0x01020304 from stdin */
read(0,&decode.ii,sizeof(decode.ii));

printf("high word = %04hx\n",decode.word.high);
printf("low word = %04hx\n",decode.word.low);

/* Reads 0x04030201 from stdin */
read(0,&decode.ii,sizeof(decode.ii));

printf("high word = %04hx\n",decode.word.high);
printf("low word = %04hx\n",decode.word.low);
}

Redirecting a file containing eight bytes, below, you can use the union
variable decode to get direct access to the two 16 bit words of the int
value read.

binfile contains: "^A^B^C^D^D^C^B^A" (quotes not in file)

CJ

Apr 21 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Simon Elliott | last post by:
I'd like to do something along these lines: struct foo { int i1_; int i2_; }; struct bar {
6
by: Neil Zanella | last post by:
Hello, I would like to know what the C standards (and in particular the C99 standard) have to say about union initializers with regards to the following code snippet (which compiles fine under...
2
by: Barry Schwarz | last post by:
Given a union of the form union { T1 m1; T2 m2;}obj; where T1 and T2 are different scalar (non-aggregate) types. The C99 standard states that obj.m1 = value; if (obj.m2 ... invokes...
10
by: Denis Pithon | last post by:
Hi, C lovers! I stuck on an union problem Here is snippet of my code .... /* two pointers of function with repsectively one and two argues */ typedef int (*dce_sn_f)(dce_t*);
2
by: Peter Dunker | last post by:
Hi, I will write ANSI C89. Is the following struct defenition correct ? I wrote it with VC6(Windows IDE) and at first no Problem. As I changed a compiler switch to 'no language extension', the...
73
by: Sean Dolan | last post by:
typedef struct ntt { int type; union { int i; char* s; }; }nt; nt n; n.i = 0;
4
by: Girish | last post by:
I have 2 differesnt defination of same Union as below and a piece of code for printing size of Union and its members.. union U { union U { int i; int j; }a;
18
by: ranjeet.gupta | last post by:
Dear ALL As we know that when we declare the union then we have the size of the union which is the size of the highest data type as in the below case the size should be 4 (For my case and...
25
by: balasam | last post by:
I want what is the application of union in C.Please explain
30
by: Yevgen Muntyan | last post by:
Hey, Why is it legal to do union U {unsigned char u; int a;}; union U u; u.a = 1; u.u; I tried to find it in the standard, but I only found that
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.