LearnFrenzy
Sign in Take a free test

Structures, Unions, Enums · Find the Error · Question 7

Error: flexible array member that is not the last member Answer, explanation & discussion

  • Find the error
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 7 Back to Exercise

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

main.c
#include <stdio.h>

struct packet {
    char data[];
    int length;
};

int main(void) {
    printf("%zu\n", sizeof(struct packet));
    return 0;
}
  • A data[] must have a size
  • B A flexible array member must be the last member of the struct Correct
  • C length must be unsigned
  • D There is no error
Correct Answer: Option B

The flexible array extends past the end of the struct, so nothing can follow it. Move it last: struct packet { int length; char data[]; };.

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.

Popular