Kodewick
Start learning →
Basic←→0 / 70

Restock the milk

#060BasicStructsPointers4 min

The corner shop is down to 2 cartons of milk, and restock(&milk, 10) adds 10. The function gets item, the address of milk; add more to its qty through it.

Should show
milk: 12
main.c
#include <stdio.h>

struct Item {
    char name[20];
    int qty;
};

void restock(struct Item *item, int more) {
    // your code here: add more to item->qty
}

int main(void) {
    struct Item milk = {"milk", 2};
    restock(&milk, 10);
    printf("%s: %d\n", milk.name, milk.qty);
    return 0;
}
Ctrl + Enter runs too
Result

Run tries the examples. When they look right, Submit.