Q:

program crashing while assigning a new node to tree node

I have written a c program for tree. 
#include<stdio.h>
#include<stdlib.h>

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

 struct node* newNode(int value){
    struct node* temp;
    temp->left = NULL;
    temp->right = NULL;
    temp->data = value;

    return temp;
 }

 int main(){
    struct node *root;
    root = newNode(60);
    root->left = newNode(40);
    root->right = newNode(80);
    root->left->left = newNode(30); // program crashes here. 
    root->left->right = newNode(50);

 }

This is a sub-part of another program that I am writing. while debugging I realized that I am getting an error at assigning newNode(30). I can't understand why ?

A:

In your newNode() function, you are doing
struct node* temp;
temp->left = NULL;     //invalid memory access
temp->right = NULL;    //invalid memory access
temp->data = value;    //invalid memory access

but, temp is not allocated any valid memory. It invokes undefined behavior when you dereference an invalid pointer.
You need to allocate memory to temp before you can dereference temp. You can make use of malloc() and family to get this done, something like,
struct node* temp = malloc (sizeof *temp);
if (temp )
{
    temp->left = NULL;
    temp->right = NULL;
    temp->data = value;
}

should get the job done.

A:

You need to allocate memory for the new node. Like
struct node* temp;
temp = malloc(sizeof(struct node));

And when your done you must remember to free the memory again.

