Sams Teach Yourself C in 21 Days

(singke) #1
Implementing Structures, Unions, and TypeDefs 265

11



  1. The structure member mysale.amountis initialized to the amount 1000.00
    (line 12).
    You can also initialize arrays of structures. The initialization data that you supply is
    applied, in order, to the structures in the array. For example, to declare an array of struc-
    tures of type saleand initialize the first two array elements (that is, the first two struc-
    tures), you could write
    1: struct customer {
    2: char firm[20];
    3: char contact[25];
    4: };
    5:
    6: struct sale {
    7: struct customer buyer;
    8: char item[20];
    9: float amount;
    10: };
    11:
    12:
    13: struct sale y1990[100] = {
    14: { { “Acme Industries”, “George Adams”},
    15: “Left-handed widget”,
    16: 1000.00
    17: }
    18: { { “Wilson & Co.”, “Ed Wilson”},
    19: “Type 12 gizmo”,
    20: 290.00
    21: }
    22: };
    This is what occurs in this code:

  2. The structure member y1990[0].buyer.firmis initialized to the string “Acme
    Industries”(line 14).

  3. The structure member y1990[0].buyer.contactis initialized to the string
    “George Adams”(line 14).

  4. The structure member y1990[0].itemis initialized to the string “Left-handed
    widget”(line 15).

  5. The structure member y1990[0].amountis initialized to the amount 1000.00(line
    16).

  6. The structure member y1990[1].buyer.firmis initialized to the string “Wilson &
    Co.”(line 18).

  7. The structure member y1990[1].buyer.contactis initialized to the string “Ed
    Wilson”(line 18).


18 448201x-CH11 8/13/02 11:17 AM Page 265

Free download pdf