Page 253 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 253

230                                    Chapter 8 Programming in C and C++


        The procedure fun( ) in §8.2 illustrated the accessing of elements of vectors in
        expressions. The expression c = d[a + b], accessed the a + b th element of the 8-
        bit 10-element vector d. When reading the assembly code generated by C, be wary of
        the implicit multiplication of the vector's precision (in bytes) when calculating offset
        addresses of elements of the vector. Because C does not check that indexes are within a
        vector, a C program must be able to implicitly or explicitly assure this to avoid nasty
        bugs, as when a vector's data is inadvertently stored outside memory allocated to a
        vector.
            The C structure mechanism can store different-sized elements. The mechanism is
         implemented by a declaration that begins with the word struct and has a definition of the
        structure within angle brackets and a list of variables of that structure type after the
        brackets, as in
                        struct { char 11; int 12; char 13;} list;
        A globally defined list can be initialized as we did with vectors, as in

                   struct { char 11; int 12; char 13;} list = {5,7,9};
        The data in a list are identified by "dot" notation, where a dot (.) means "element." For
        instance, list. 11 is the 11 element of the list list. If P is a pointer to a struct,
        then arrow notation, such as P->11, can access the element 11 of the list. The
        typedef statement, though it can be used to create a new data type in terms of existing
        data types, is often used with structs. If typedef a struct { char 11; int 12;
        char 13;} list; is written, then list is a data type, like int or char, and can be
        used in declarations such as list b; that declare b to be an instance of type list.
        We will find the typedef statement to be quite useful when a struct has to be
        declared many times and pointers to it need to be declared as well. A structure can have
        bit fields, which are unsigned integer elements having less than 16 bits. Such a
        structure as
                           struct {unsigned a:l, b:2, c:3;}l;
        has a one-bit field 1. a, two-bit field 1. b, and three-bit field 1. c. A linked list
        structure, a list in which some elements are addresses of (the first word in) other lists, is
        flexible and powerful and is widely used in advanced software.
            We normally think of an array as a two-dimensional pattern, as in
                             1             2             3
                            4             5              6
                            7              8             9
                             10            11            12
        An array is considered a vector whose elements are themselves vectors, and C syntax
        reflects this philosophy. For instance, the global declaration

                   int arl[4][3]={{l,2,3},{4,5,6},{7,8,9},{10,ll,12}>;
        allocates and initializes a row major ordered array (rows occupy consecutive memory
        words) ar 1, and a = ar 1 [ i ] [ j ]; puts the row-z column-,/ element of ar 1 into a.
            A table is a vector of identically formatted structs. Tables often store characters,
        where either a single character or a collection of n consecutive characters is considered an
   248   249   250   251   252   253   254   255   256   257   258