Presentation is loading. Please wait.

Presentation is loading. Please wait.

Structures.

Similar presentations


Presentation on theme: "Structures."— Presentation transcript:

1 Structures

2 Parallel Arrays Inventory
One array keeps track of cost One array keeps track of number in inventory What if there were many parallel items to keep track of? Would you want to keep track of 10, 20, 30 parallel arrays? Why not?

3 Structures Group many record items into one element Inventory:
Item Name: Shirt Item Number: 1232 Cost: $20.00 Num in Inventory: 10

4 Structures typedef struct { char name[20]; int number; double cost;
int num_in_inventory; } inventory_item_t;

5 Structures typedef struct { type name; … type name; } struct_type;
goes after #include and #define – not inside of main type can be another structure data type typedef struct { char first[20]; char last[20]; } name_t; typedef struct { name_t name; int id; } student_t;

6 Declaring and Initializing
typedef does not allocate any memory inventory_item_t shirts; .name .number .cost .num_in_inventory

7 Declaring and Initializing
shirts.number = 1232; shirts.cost = 20.00; shirts.num_in_inventory = 10; //name??? .name .number .cost .num_in_inventory 1232 20.00 10

8 Declaring and Initializing
shirts.number = 1232; shirts.cost = 20.00; shirts.num_in_inventory = 10; strcpy(shirts.name, “Shirt”); .name .number .cost .num_in_inventory 1232 20.00 10

9 Accessing Print “There are 10 of item: shirt left in the inventory.”

10 Accessing Print “There are 10 of item: shirt left in the inventory.”
printf(“There are %d of item: %s left in the inventory”, shirts.num_in_inventory, shirts.name);

11 Structures and Functions
Write a function to print all information about an inventory item

12 Structures and Functions
Write a function to print all information about an inventory item void print_item(inventory_item_t item) { printf(“Item: %s\n”, item.name); printf(“Item Number: %d\n”, item.number); printf(“Cost: %lf\n”, item.cost); printf(“Number Remaining: %d\n”, item.num_in_inventory); }

13 Structures and Functions
Call the function inventory_item_t shirts; print_item(shirts);

14 Structures as Output Parameters
Write a function to “sell” an item by deducting 1 from the number of the item left in the inventory Return 1 if the sell was successful – 0 otherwise How would we call this function?

15 Structures as Output Parameters
How would we call this function? inventory_item_t shirts; sell_item(shirts); //OK?

16 Structures as Output Parameters
How would we call this function? inventory_item_t shirts; int sell_ok; sell_ok = sell_item(&shirts); if(sell_ok) { printf(“Item sold\n”); } else { printf(“Problem encountered! Item not sold.\n”); }

17 Structures as Output Parameters
int sell_item(inventory_item_t *to_sell) { int sell_ok; if((*to_sell).num_in_inventory > 0) { //(*to_sell) is important! (*to_sell).num_in_inventory = (*to_sell).num_in_inventory – 1; sell_ok = 1; } else { sell_ok = 0; } return (sell_ok);

18 Component Selection Operator
(*to_sell).num_in_inventory is the same as to_sell->num_in_inventory int sell_item(inventory_item_t *to_sell) { int sell_ok; if(to_sell->num_in_inventory > 0) { to_sell->num_in_inventory = to_sell->num_in_inventory – 1; sell_ok = 1; } else { sell_ok = 0; } return (sell_ok);

19 Returning a Structure inventory_item_t getItem() { inventory_item_t new_item; printf(“Enter name:”); scanf(“%s”, new_item.name); printf(“Enter number:”); scanf(“%d”, &new_item.number); printf(“Enter cost:”); scanf(“%lf”, &new_item.cost): printf(“Enter number in inventory:”); scanf(“%d”, %new_item.num_in_inventory); return (new_item); }

20 Arrays of Structures inventory_item_t items[20];
Access the name of item 5

21 Arrays of Structures inventory_item_t items[20];
Access the name of item 5 items[4].name; items[4].num_in_inventory = items[4].num_in_inventory – 1; items[5].cost = ; scan_item(&items[10);


Download ppt "Structures."

Similar presentations


Ads by Google