Files
VCCPlusOSS/examples/cmoc/manual/cargo.c
T

40 lines
884 B
C

/* SPDX-License-Identifier: GPL-3.0-or-later */
/* Count a delivery on two planets. The manifest stays in guest RAM. */
#include <cmoc.h>
#include <coco.h>
struct Shipment
{
const char *destination;
byte crates;
byte mealsPerCrate;
};
static const struct Shipment manifest[] =
{
{ "EARTH", 12, 20 },
{ "MARS", 8, 15 }
};
/* A pointer lets us read one row without copying the whole structure. */
static word mealsFor(const struct Shipment *shipment)
{
return (word) shipment->crates * shipment->mealsPerCrate;
}
int main(void)
{
word total = 0;
byte row;
width(40);
for (row = 0; row < sizeof(manifest) / sizeof(manifest[0]); ++row)
{
word meals = mealsFor(&manifest[row]);
printf("%s: %u MEALS\n", manifest[row].destination, meals);
total += meals;
}
printf("TOTAL: %u MEALS\n", total);
return 0;
}