Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Parallel Arrays
Inventory
One array keeps track of cost
One array keeps track of number in inventory
Inventory:
Item Name: Shirt
Item Number: 1232
Cost: $20.00
Num in Inventory: 10
Structures
typedef struct {
char name[20];
int number;
double cost;
int num_in_inventory;
} inventory_item_t;
Structures
typedef struct {
type name;
type name;
} struct_type;
inventory_item_t shirts;
.name
.number
.cost
.num_in_inventory
Declaring and Initializing
shirts.number = 1232;
shirts.cost = 20.00;
shirts.num_in_inventory = 10;
//name???
.name
1232
.number
20.00
.cost
10
.num_in_inventory
Declaring and Initializing
shirts.number = 1232;
shirts.cost = 20.00;
shirts.num_in_inventory = 10;
strcpy(shirts.name, Shirt);
.name
1232
.number
20.00
.cost
10
.num_in_inventory
Accessing
Print There are 10 of item: shirt left in the
inventory.
Accessing
Print There are 10 of item: shirt left in the
inventory.
inventory_item_t shirts;
print_item(shirts);
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?
Structures as Output Parameters
How would we call this function?
inventory_item_t shirts;
sell_item(shirts); //OK?
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);
}
Structures as Output Parameters