Scor­pio News

  

October–December 1988 – Volume 2. Issue 4.

Page 14 of 35

struct obj{ int x;
int y;
char obj_type;
short rotation; };

In this case a structure type called obj refers to a pair of integers x,y (the co-ordinates), a character obj_type (the type of object) and a short integer rotation (the direction of the object). Now we can define variables of type “struct obj”, or pointers to such a structure etc.

Unions are similar to structures except that all items are at the same place and thus only one item is valid at a time. The main use of unions is for shared allocation of data to reduce overall memory usage.

Aerays of any type are allowed using square brackets (“[” & “]”) around the index. Multi-dimension arrays use multiple brackets. For example:

chararray_of_chars[16];
inttwo_dimensional_array[10][10];

The name of an array so defined translates as simply a constant of type pointer_to_element_type, so array_of_chars above is a constant character pointer (written as “char * array_of_chars”) whose value is the address of the start of the array. This fact is not important for the use of arrays, but it does mean that a variable of a pointer type can be used to Point to the base of an array, for example:

char * string:         /* define variable string as a character pointer */
if ( string[4] == 'h' ) do_something();     /* test the fifth character */

There is no string type in C. A String is stored in an array of chars, usually terminated by a NULL. Constant strings can be entered, e.g.

printf ( "Hello World" );

However, the string here evaluates to a constant of type “pointer to char” which is the address of an area of memory containing the twelve bytes (including a terminating NULL). Consequently a statement such as

if ( input_string == "YES" ) ....

makes no sense at all in C. The way round this is to use one of the standard functions supplied in the library.


Page 14 of 35