Union (computer Science) - Syntax and Example

Syntax and Example

In C and C++, the syntax is:

union { <1st variable name>; <2nd variable name>; . . . ; } ;

A structure can also be a member of a union, as the following example shows:

union name1 { struct name2 { int a; float b; char c; } svar; int d; } uvar;

This example defines a variable uvar as a union (tagged as name1), which contains two members, a structure (tagged as name2) named svar (which in turn contains three members), and an integer variable named d.

Unions may occur within structures and arrays,and vice versa:

struct { int flags; char *name; int utype; union { int ival; float fval; char *sval; } u; } symtab;

The number ival is referred to as symtab.u.ival and the first character of string sval by either of *symtab.u.sval or symtab.u.sval.

Read more about this topic:  Union (computer Science)

Famous quotes containing the words syntax and:

    Syntax and vocabulary are overwhelming constraints—the rules that run us. Language is using us to talk—we think we’re using the language, but language is doing the thinking, we’re its slavish agents.
    Harry Mathews (b. 1930)