A packing program stores two item counts inside a structure. Trace the following C code and complete the table at the two indicated checkpoints. Enter exact integer counts without units.
struct Batch { int counts[2]; }; int trial(struct Batch b) { b.counts[0] += 4; b.counts[1] = b.counts[0] + b.counts[1]; return b.counts[1]; } void apply(struct Batch *b) { b->counts[0] += 4; b->counts[1] = b->counts[0] + b->counts[1]; } void run(void) { struct Batch batch = {{7, 2}}; int result = trial(batch); /* Checkpoint 1 */ apply(&batch); /* Checkpoint 2 */ }
Both count columns refer to the caller's variable batch, and the result column refers to the caller's variable result.
Unlock full course notes by signing up
Following a reassigned pointer parameter
An inventory program runs the following C code. Find the final values of a.units and b.units immediately before main returns. Report both quantities in inventory units as exact integers, to zero decimal places.
Qualifying the addressed structure
A reporting function receives a pointer to a structure whose only member is an integer count. Its interface should prevent direct member assignments through that pointer while allowing the function to reassign its local pointer parameter. Place the C keyword ________ immediately before struct Sample in the parameter declaration.
Tracing shared data through a copied structure
A C program stores a record count and a pointer to an external integer in a structure. After the following code executes, which statements are true immediately before main returns? Select all that apply.
Updating one stock record through two parameters
An inventory simulation uses the following C function. Each units member records a number of items. Treat the two scenarios below as independent runs, and report all three requested final counts as exact integers in items.
In the shared-object scenario, the call is:
In the separate-object scenario, the call is: