LearnFrenzy
Sign in Take a free test

Control Instructions · Find the Error · Question 8

Error: goto jumping past a variable length array Answer, explanation & discussion

  • Find the error
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 8 Back to Exercise

The program below has a bug. Which statement correctly describes it?

main.c
#include <stdio.h>

int main(void) {
    int n = 3;
    goto fill;
    int values[n];
fill:
    for (int i = 0; i < n; i++)
        values[i] = i;
    printf("%d\n", values[2]);
    return 0;
}
  • A n must be a constant
  • B The goto jumps into the scope of the variable length array values, past its declaration, which C forbids Correct
  • C A label cannot be followed by a for loop
  • D There is no bug
Correct Answer: Option B

A VLA gets its storage and size when its declaration executes; jumping over it would leave values without either. The compiler rejects the jump. Declare the array before the goto, or restructure without it.

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