LearnFrenzy
Sign in Take a free test

Typedef · Find the Error · Question 1

Error: using the typedef name inside its own struct Answer, explanation & discussion

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

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

main.c
#include <stdio.h>

typedef struct {
    int value;
    Node *next;
} Node;

int main(void) {
    Node n = {1, NULL};
    printf("%d\n", n.value);
    return 0;
}
  • A NULL cannot initialise a pointer member
  • B Node does not exist until the end of the typedef; give the struct a tag and write struct node *next Correct
  • C value must come after next
  • D There is no bug
Correct Answer: Option B

The alias is introduced only after the closing brace. Write typedef struct node { int value; struct node *next; } Node;, or declare typedef struct node Node; before the struct definition.

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