Why is this simple hello world code segfaulting?

Posted by socks on Stack Overflow See other posts from Stack Overflow or by socks
Published on 2010-12-21T22:45:22Z Indexed on 2010/12/21 22:54 UTC
Read the original article Hit count: 131

Filed under:

Excuse the beginner level of this question. I have the following simple code, but it does not seem to run. It gets a segmentation fault. If I replace the pointer with a simple call to the actual variable, it runs fine... I'm not sure why.

struct node
{
 int x;
 struct node *left;
 struct node *right;
};

int main()
{
 struct node *root;
 root->x = 42;
 printf("Hello world. %d", root->x);
 getchar();
 return 0;
}

What is wrong with this code?

© Stack Overflow or respective owner

Related posts about c