A sensor driver stores its current behaviour as a function pointer inside a struct. Each behaviour updates the level and may replace itself with the other behaviour for the next call. The program below calls m->step(m) six times.
struct meter; void fast(struct meter *m); void slow(struct meter *m); struct meter { int level; void (*step)(struct meter *); }; void fast(struct meter *m) { m->level += 3; if (m->level >= 7) m->step = slow; } void slow(struct meter *m) { m->level -= 1; if (m->level == 5) m->step = fast; } int main(void) { struct meter m[1] = { { 1, fast } }; for (int k = 1; k != 7; k++) m->step(m); return 0; }
For each call, enter the name of the function that runs (fast or slow), the value of level after that call returns, and the name of the function that step points to after the call. Enter integers for the level.
Unlock full course notes by signing up
Follow a callback through repeated calls
A callback is a function supplied to another function for that receiving function to invoke. Execute this C program once from the shown initial state. Immediately before main returns, find the local variable result and the global variable calls. Give both as exact integers with zero decimal places.
Repair a callback argument hidden by an alias
A type alias supplies another name for an existing type. This C program intends to call add_two on 9 through run_transform, but its final call has an argument-type error. Which single edit makes the program well-typed and preserves that intended operation? Leave the typedef and function interfaces unchanged.
Read an array of function pointers inside out
A logging module declares the following table of formatter functions, each chosen by a message level:
Arrange the steps of the inside-out reading rule in the order you apply them to decode this declaration, from the first look at the code to the final description of what table is.
Separate pointer conversions from valid calls
An indirect call invokes a function through a pointer expression. Consider these declarations in ISO C, immediately after initialization and before either pointer is reassigned. Which statements correctly describe the pointer expressions and proposed calls? Select all that apply.