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.
typedef int (*Transform)(int); int add_two(int value) { return value + 2; } int run_transform(Transform fn, int value) { return fn(value); } int main(void) { Transform fn = add_two; return run_transform(&fn, 9); }
Replace Transform fn = add_two; with Transform *fn = add_two;.
Replace run_transform(&fn, 9) with run_transform(fn(9), 9).
Replace run_transform(&fn, 9) with run_transform(fn, 9).
Replace return fn(value); with return (*fn)(value);.
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.
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.
Trace a struct whose function pointer member switches itself
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.
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.