ProgrammingHardIBM · Deloitte pattern

Assume a 64-bit LP64 ABI. What is printed?

Assume a 64-bit LP64 ABI. What is printed?
struct Inventory {
    char name[16];
    int quantity;
    double price;
};
void restock(struct Inventory *item, int amount) {
    item->quantity += amount;
}
int main(void) {
    struct Inventory stock[2] = {
        {"Widget", 10, 2.5},
        {"Gadget", 5, 9.9}
    };
    restock(&stock[0], 20);
    restock(&stock[1], -2);
    printf("%d %d", stock[0].quantity, stock[1].quantity);
}
  1. A. 10 5
  2. B. 30 3
  3. C. 20 -2
  4. D. Undefined behaviour: restock cannot be called twice

Correct answer: B. 30 3

Each restock() call modifies the specific struct it's given the address of, through item->quantity += amount. stock[0].quantity: 10+20=30. stock[1].quantity: 5+(-2)=3.

More C Programming questions

Exams that ask this topic