LearnFrenzy
Sign in Take a free test

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

Error: a struct that contains itself Answer, explanation & discussion

  • Find the error
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 1 Back to Exercise

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

main.c
#include <stdio.h>

struct node {
    int value;
    struct node next;
};

int main(void) {
    printf("%zu\n", sizeof(struct node));
    return 0;
}
  • A value must come after next
  • B struct node next; – a struct cannot contain itself; use struct node *next; Correct
  • C sizeof cannot be used on a struct
  • D There is no error
Correct Answer: Option B

While its definition is still open, struct node is an incomplete type with unknown size, so it cannot be a member. A pointer to it has a known size: struct node *next;.

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