Programming Tutorial
Part 4
by J.F.R. "Frank" Slinkman
Before I get into the guts of this issue's installment, an important word of advice:
As you go through this series, EVERY time a standard library function is referred to or used in a program, you need to look at the documentation for that function, and use the docs as a supplement to the article text. You'll never fully understanding what's going on if you don't do this.
O.K. Let's examine C's powerful, built-in data organization tools.
When it comes to storing data in RAM, BASIC offers only one way (the array) to organize multiple data items into a single logical unit. The limitation of arrays is they can contain only one type of data.
However, if you're sneaky, you can logically combine different data types in RAM by opening a random access disk file, and having two subroutines -- one (subroutine A) to field its buffer with the actual data elements, and one (subroutine B) to field the entire buffer as a single element.
To store data, you would call subroutine A, and fill the buffer with data just as you would for a disk file write. Then call subroutine B, and copy the entire buffer to an element of an array of strings.
The data can later be extracted from the array by calling subroutine B, LSETting an array element into the buffer, calling subroutine A, and accessing the data as if you'd just done a disk file read.
This method treats RAM like a disk file. The array is the "file," the array elements are "records," subroutine B's FIELD command defines the records, and subroutine A's FIELD command defines the fields within the records.
If you can visualize storing data in RAM this way, you're 99% of the way toward understanding how C manages data elements called "structs." Interestingly, and in keeping with the above analogy, structs are sometimes also referred to as "records."
Fortunately, handling structs in C is much easier than the above-described BASIC method.
In addition to arrays and structs, C offers one additional method of data organization, the "union."
A "union" provides a means of looking at the same data in more than one way.
In C, these three methods of data organization --arrays, unions and structs -- are extremely flexible and versatile.
You can create arrays of structs, for example, or structs of arrays. You can have unions of arrays and structs; structs which contain arrays and unions, and even other structs, just to mention a few of the possibilities.
A "union" of two data elements is declared as follows:
union type_name {
type type } union__name;
memberjname; member_name;
The variables included in the union are called the union's "members."
"Type_name" is optional, and would normally be used only if more than one of the same type of union is to be declared.
If "type_name" is used, it becomes a "typedef' --a sort of template which makes it easy, elsewhere in the program, to declare other variables to be unions of the same type. In other words, it defines "union type_name" to be a data type just as "char," "int," and "double" are data types.
Also, "unionjname" is optional. If you just want to define the union as a data type for later use, you would define the union similarly to:
typedef union type_name
{ type type
member_name; member_name;
Henceforth, anytime you declare a variable to be of type union type_name, the compiler would know the type and size of the data, and how to handle it.
The following program illustrates the use of a data union:
#include #include
#option ARGS #option REDIRECT #option FIXBUFS ^option MAXFILES
union {
mainO {
Post a comment