c - Tree traversal - Segmentation fault error -


code :

#include<stdio.h> #include<malloc.h>  typedef struct tree {     char data;     struct tree *left;     struct tree *right; }*pos;  pos stack[30]; int top=-1;  pos newnode(char b) {      pos temp;     temp=(struct tree*)malloc(sizeof(struct tree));     temp->data=b;     temp->left=null;     temp->right=null;     return(temp); }  void push(pos temp) {     stack[++top]=temp; }  pos pop() {     pos p;     p=stack[top--];     return(p); }  void inorder(pos t) {     if(t!=null)     {         inorder(t->left);         printf("%s",t->data);         inorder(t->right);     } } void preorder(pos t) {     if(t!=null)     {         printf("%s",t->data);         preorder(t->left);         inorder(t->right);     } }  void postorder(pos t) {     if(t!=null)     {          postorder(t->left);         postorder(t->right);         printf("%s",t->data);     } }  void main() {     char *a;     pos temp,t;     int j,i;     puts("enter expression :");     scanf("%s",&a);     for(i=0;a[i]!='\0';i++)     {         if(a[i]=='*' || a[i]=='/' || a[i]=='+' || a[i]=='-')         {             temp=newnode(a[i]);             temp->right=pop();             temp->left=pop();             push(temp);         }         else         {             temp=newnode(a[i]);             push(temp);         }     }     inorder(temp);     printf("\n");     preorder(temp);     printf("\n");     postorder(temp); } 

error : segmentation fault

this code construction of binary tree traversal , conversion of postfix infix , prefix. dont know going wrong keeps saying same fault. can me ?

    scanf("%s",&a); // problem.  

scanf accepts pointer , passing address of pointer. have pass pointer.

    scanf("%s",a); // since pointer, use a. 

and not allocating memory. need allocate memory hold scanned string thing this...

    = (char*)malloc(sizeof(*a) * max_size); 

Comments

Popular posts from this blog

java - JavaFX 2 slider labelFormatter not being used -

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -