LearnFrenzy
Sign in Take a free test

Complicated Declarations · Predict the Output · Question 12

C23 typeof to declare a function-pointer array Answer, explanation & discussion

  • Output
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 12 Back to Exercise

What is the output of the following program, compiled as C23?

main.c
#include <stdio.h>

int up(int x) { return x + 10; }
int down(int x) { return x - 1; }

int main(void) {
    typeof(up) *steps[] = {up, down, down};
    typeof(steps[0]) first = steps[0];
    int v = first(0);
    for (int i = 1; i < 3; i++)
        v = steps[i](v);
    printf("%d %zu\n", v, sizeof steps);
    return 0;
}
  • A 8 24 Correct
  • B 10 24
  • C 8 3
  • D 9 24
Correct Answer: Option A

typeof(up) * is a pointer to a function like up, so steps is an array of 3 function pointers (24 bytes). 0 + 10 − 1 − 1 = 8.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links