RecordsUnionsNamespaces - Records, Unions, and Namespaces...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Records, Unions, and Namespaces Beta Draft - Do not distribute © 2001, By Randall Hyde Page 483 Recor ds, Unions, and Name Spaces Chapter Five 5.1 Chapter Overview This chapter discusses ho w to declare and use record (structures), unions, and name spaces in your pro - grams. After strings and arrays, records are among the most commonly used composite data types; indeed, records are the mechanism you use to create user -defi ned composite data types. Man y assembly language programmers ne v er bother to learn ho w to use records in assembly language, yet w ould ne v er consider not using them in high le v el language programs. This is some what inconsistent since records (structures) are just as useful in assembly language programs as in high le v el language programs. Gi v en that you use records in assembly language (and especially HLA) in a manner quite similar to high le v el languages, there really is no reason for e xcluding this important tool from your programmer’ s tool chest. Although you’ ll use unions and name spaces f ar less often than records, their presence in the HLA language is crucial for man y adv anced applications. This brief chapter pro vides all the information you need to successfully use records, unions, and name spaces within your HLA programs. 5.2 Records Another major composite data structure is the P ascal r ecor d or C/C++ structur e 1 . The P ascal terminol - ogy is probably better , since it tends to a v oid confusion with the more general term data structur e . Since HLA uses the term “record” we’ ll adopt that term here. Whereas an array is homogeneous, whose elements are all the same, the elements in a record can be of an y type. Arrays let you select a particular element via an inte ger inde x. W ith records, you must select an element (kno wn as a fi eld ) by name. The whole purpose of a record is to let you encapsulate dif ferent, b ut logically related, data into a single package. The P ascal record declaration for a student is probably the most typical e xample: student = record Name: string [64]; Major: integer; SSN: string[11]; Midterm1: integer; Midterm2: integer; Final: integer; Homework: integer; Projects: integer; end; Most P ascal compilers allocate each fi eld in a record to contiguous memory locations. This means that P ascal will reserv e the fi rst 65 bytes for the name 2 , the ne xt tw o bytes hold the major code, the ne xt 12 the Social Security Number , etc. In HLA, you can also create structure types using the RECORD/ENDRECORD declaration. Y ou w ould encode the abo v e record in HLA as follo ws: type student: Name: char[65]; Major: int16; SSN: char[12]; 1. It also goes by some other names in other languages, but most people recognize at least one of these names. 2. Strings require an extra byte, in addition to all the characters in the string, to encode the length.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Chapter Five Volume Three Page 484 © 2001, By Randall Hyde Beta Draft - Do not distribute Midterm1: int16; Midterm2: int16; Final: int16; Homework: int16; Projects: int16; endrecord; As you can see, the HLA declaration is v ery similar to the Pascal declaration.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 08/08/2011 for the course CS 101 taught by Professor Jitenderkumarchhabra during the Summer '11 term at National Institute of Technology, Calicut.

Page1 / 18

RecordsUnionsNamespaces - Records, Unions, and Namespaces...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online