Following counts stored inside a structure

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.


C
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.

CheckpointFirst countSecond countResult
1
2

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 Passing Structures to Functions Questions: