Following a copied structure with a shared target

An inventory program copies a structure containing integer counts, an embedded array and a pointer member. Trace the following C17 code, executed inside a function.


C
int stock = 18;

struct Bin {
    int count;
    int *shared;
    int local[2];
};

struct Bin a = { 5, &stock, { 2, 7 } };
struct Bin b = a;

b.count += 4;
b.local[0] += 6;
*b.shared -= b.count;
a.count = *a.shared + a.local[0];
*b.shared += b.local[0] - a.local[1];


Report the final values of stock, a.count and b.local[0]. Every requested value is a count of items; enter exact integers.

Your answers

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Structure Types Questions: