New to c other languages such as pascal provide two

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: introduce an additional tag field: struct NODE { int is_leaf; union { struct { struct NODE *left; struct NODE *right; } internal; double data; } info; }; 158 CHAPTER 3. MACHINE-LEVEL REPRESENTATION OF C PROGRAMS where the field is_leaf is 1 for a leaf node and is 0 for an internal node. This structure requires a total of 12 bytes: 4 for is_leaf, and either 4 each for info.internal.left and info.internal.right, or 8 for In this case, the savings gain of using a union is small relative to the awkwardness of the resulting code. For data structures with more fields, the savings can be more compelling. Unions can also be used to access the bit patterns of different data types. For example, the following code returns the bit representation of a float as an unsigned: 1 2 3 4 5 6 7 8 9 unsigned float2bit(float f) { union { float f; unsigned u; } temp; temp.f = f; return temp.u; }; In this code we store the argument in the union using one data type, and access it using another. Interestingly...
View Full Document

Ask a homework question - tutors are online