title: Segmentation fault while implementing stack as an array
body: <p>This is a menu-driven program that carries out basic stack operations using arrays in the C programming language. The functions that are performed are push, pop, peep,isempty and isfull.</p>
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  
  
  struct stack
  {
      long int top;
      long int size;
      char* key;
  };
  
  
  int is_empty(struct stack *s)  //check if its empty
  {
      if(s-&gt;top==-1)
      {
              return -1;
      }
      else
      {
          return 1;
      }
  }
  int is_full(struct stack *s)  //check if its full
  {
      if (s-&gt;top ==s-&gt;size-1)
      {
          return -1;
      }
      else
      {
          return 1;
      }
  }
  
  
  void push(struct stack *s, char x)  //pushes into stack
  {
  int check;
  check = is_full(s);
  if(check==-1)
  {
      printf(&quot;-1\n&quot;);
     
  }
  else
  {
  s-&gt;top = s-&gt;top+1;
  s-&gt;key[s-&gt;top]=x;
  }
  }
  
   
  void pop(struct stack *s)   //deletes the last element
  {
  int check;
  check = is_empty(s);
  if(check==-1)
  {
      printf(&quot;-1\n&quot;);
      
      
  }
  else
  {
  char k;
  k = s-&gt;key[s-&gt;top];
  printf(&quot;%c\n&quot;,k);
  s-&gt;top--;
  }
  }
  
  void peep(struct stack *s)  //prints the last element without deleting
  {   int check;
  char k;
      check = is_empty(s);
      if (check == -1)
      {
          printf(&quot;-1\n&quot;);
      }
      else
      {
              k = s-&gt;key[s-&gt;top];
             printf(&quot;%c \n&quot;,k);
      }    
  }
  
  int main()
  {
  
  
      char ch;
      char x;
      long int n;
      struct stack *s;
      scanf(&quot;%ld &quot;, &amp;n);
      s-&gt;size = n;  //initialise the size 
      s-&gt;top = -1;  //setting as -1 base case
      s-&gt;key= (char *)malloc(n*sizeof(char));  //dynamic allocation of keys
      while(1)
      {
          scanf(&quot;%c &quot;,&amp;ch);
          switch(ch)
          {
          case 'i':
                  scanf(&quot;%c &quot;,&amp;x);
                  push(s,x);
                  break;
          case 'd':pop(s);
                  break;
          case 'p':peep(s);
                  break;
          case 't':exit(0); //termination case
      }  
  }
  return 0;
  }
  </code></pre>
  <p>This is a C program that is working for me in some online compilers but in VScode and other compilers, it's showing a segmentation fault without any output. This is an implementation of stack using arrays. Is it a problem with any of the scanf functions?</p>