Structures



  • A structure is a collection of one or more variables, possibly of different types, grouped together under a single name for convenient handling.


  • Example : Let us create a few structures suitable for graphics. The basic object is a point, which we will assume has an x coordinate and a y coordinate, both integers.


  • struct point {
           int x;
           int y;
       };
    

  • The keyword struct introduces a structure declaration, which is a list of declarations enclosed in braces. An optional name called a structure tag may follow the word struct.


  • The variables named in a structure are called members. A structure member or tag and an ordinary (i.e., non-member) variable can have the same name without conflict, since they can always be distinguished by context.


  • A struct declaration defines a type. The right brace that terminates the list of members may be followed by a list of variables, just as for any basic type. Example : struct { ... } x, y, z;


  • A structure declaration that is not followed by a list of variables reserves no storage; it merely describes a template or shape of a structure. If the declaration is tagged, however, the tag can be used later in definitions of instances of the structure. For example, given the declaration of point above, struct point pt; defines a variable pt which is a structure of type struct point.


  • A member of a particular structure is referred to in an expression by a construction of the form structure-name.member The structure member operator ``.'' connects the structure name and the member name. Example : printf("%d,%d", pt.x, pt.y);



Q. Demonstrate Nested Structures


  •   struct rect {
           struct point pt1;
           struct point pt2;
       };

  • The rect structure contains two point structures. If we declare screen as 
       struct rect screen;
    then 
       screen.pt1.x




Structures and Functions



  • The only legal operations on a structure are copying it or assigning to it as a unit, taking its address with &, and accessing its members.


  • Structures may not be compared.


  •    /* makepoint:  make a point from x and y components */
       struct point makepoint(int x, int y)
       {
           struct point temp;
           temp.x = x;
           temp.y = y;
           return temp;
       }
    

  • The above can now be used to initialize any structure dynamically, or to provide structure arguments to a function:


  •    struct rect screen;
       struct point middle;
       struct point makepoint(int, int);
       screen.pt1 = makepoint(0,0);
       screen.pt2 = makepoint(XMAX, YMAX);
       middle = makepoint((screen.pt1.x + screen.pt2.x)/2,
                          (screen.pt1.y + screen.pt2.y)/2);
    




Arrays of Structures



  • Consider writing a program to count the occurrences of each C keyword. We need an array of character strings to hold the names, and an array of integers for the counts. One possibility is to use two parallel arrays, keyword and keycount. char *keyword[NKEYS]; int keycount[NKEYS];


  • Each keyword is a pair: char *word; int cout;


  • The structure declaration 
       struct key {
           char *word;
           int count;
       } keytab[NKEYS];
    declares a structure type key, defines an array keytab of structures of this type, and sets aside
    storage for them.
    

  • Each element of the array is a structure. This could also be written 
       struct key {
           char *word;
           int count;
       };
       struct key keytab[NKEYS];
    



Typedef



  • C provides a facility called typedef for creating new data type names. For example, the declaration typedef int Length;


  • The type Length can be used in declarations, casts, etc., in exactly the same ways that the int type can be:


  •    Length len, maxlen;
       Length *lengths[];




Unions



  • A union is a variable that may hold (at different times) objects of different types and sizes, with the compiler keeping track of size and alignment requirements.


  • The purpose of a union - a single variable that can legitimately hold any of one of several types.


  • The syntax is based on structures: 
       union u_tag {
           int ival;
           float fval;
           char *sval;
       } u;
    

  • Syntactically, members of a union are accessed as 
      union-name.member
    or
      union-pointer->member
    just as for structures. 

  • Unions may occur within structures and arrays, and vice versa. The notation for accessing a member of a union in a structure (or vice versa) is identical to that for nested structures.


  • struct {
           char *name;
           int flags;
           int utype;
           union {
               int ival;
               float fval;
               char *sval;
           } u;
       } symtab[NSYM];