Hi so i am going through my code step by step to avoid missi

Hi, so i am going through my code step by step to avoid missing up.

here my code crashes when i enter my input

if you could take a look and tell me what causes crashing my program


#include<stdio.h>
#include<string.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
#define MAX 300

typedef struct StackStruct
{
char* expression; /* pointer to dynamic array */
int size; /* amount of space allocated */
int top; /* top of stack indicator */
} Stack;

void init (Stack* s)
{
s->size = 2;
s->expression = (char*) malloc ( sizeof (char) * s->size );
s->top = 0;
}

void push (Stack* s, char val)
{
/* check if enough space currently on stack and grow if needed */

/* add val onto stack */
s->expression[s->top] = val;
s->top = s->top + 1;
}

int isEmpty (Stack* s)
{
if ( s->top == 0)
return TRUE;
else
return FALSE;
}


int top (Stack* s)
{
return ( s->expression[s->top-1] );
}

void pop (Stack* s)
{
s->top = s->top - 1;
}


int checkBalanced( char expression[] )
{
Stack *s;
int i;
char paranthesis;
for( i=0; i<strlen(expression); i++){
   
       paranthesis=expression[i];
   if(paranthesis==\'{\'||paranthesis==\'<\'||paranthesis==\'[\'||paranthesis==\'(\')
   push (&s,paranthesis);
     
   else if(paranthesis==\'}\'||paranthesis==\'>\'||paranthesis==\']\'||paranthesis==\')\')
   {
     
      if(isEmpty (&s)){
          return FALSE;
           }
           else{
              pop (&s);
           }
       }
}
  
   return isEmpty (s) ? TRUE:FALSE;
}

int main(int argc,char *argv[]){
   Stack *s;
   init (&s);

   char expression[300];
fgets(expression, MAX, stdin);
  
checkBalanced(expression);


  

  

return 0;
  
}

Solution

Try this code and let me know if it works:

#include<stdio.h>
#include<string.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
#define MAX 300
typedef struct StackStruct
{
char* expression; /* pointer to dynamic array */
int size; /* amount of space allocated */
int top; /* top of stack indicator */
} Stack;
void init (Stack* s)
{
s->size = 2;
s->expression = (char*) malloc ( sizeof (char) * s->size );
s->top = 0;
}
void push (Stack* s, char val)
{
/* check if enough space currently on stack and grow if needed */
/* add val onto stack */
s->expression[s->top] = val;
s->top = s->top + 1;
}
int isEmpty (Stack* s)
{
if ( s->top == 0)
return TRUE;
else
return FALSE;
}

int top (Stack* s)
{
return ( s->expression[s->top-1] );
}
void pop (Stack* s)
{
s->top = s->top - 1;
}

int checkBalanced( char expression[] )
{
Stack *s;
int i;
char paranthesis;
for( i=0; i<strlen(expression); i++){

paranthesis=expression[i];
if(paranthesis==\'{\'||paranthesis==\'<\'||paranthesis==\'[\'||paranthesis==\'(\')
push (s,paranthesis);

else if(paranthesis==\'}\'||paranthesis==\'>\'||paranthesis==\']\'||paranthesis==\')\')
{

if(isEmpty (s)){
return FALSE;
}
else{
pop (s);
}
}
}
  
return isEmpty (s) ? TRUE:FALSE;
}
int main(int argc,char *argv[]){
Stack *s;
init (s);
char expression[300];
fgets(expression, MAX, stdin);
  
checkBalanced(expression);

  
  
return 0;
  
}

Hi, so i am going through my code step by step to avoid missing up. here my code crashes when i enter my input if you could take a look and tell me what causes
Hi, so i am going through my code step by step to avoid missing up. here my code crashes when i enter my input if you could take a look and tell me what causes
Hi, so i am going through my code step by step to avoid missing up. here my code crashes when i enter my input if you could take a look and tell me what causes

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site