title: implementing a dynamically sized stack using an array,elements printing error
body: <p>The structure stack has a pointer 'a' to a dynamically allocated array (used to hold the contents of the stack), an integer 'maxSize' that holds the size of this array (i.e the maximum number of data that can be held in this array) and an integer 'top' which stores the array index of the top element in the stack.</p>
  
  <p>In the initstack function intialize the value of top to -1 and initialize the value of maxSize.
  Print the message “Stack is full” in the push function when an attempt is made to push a data into a full stack.
  Print the message “Stack is empty” in the pop function and return the value -1000 when an attempt is made to pop data from an empty stack.</p>
  
  <p>In the initstack function intialize the value of top to -1 and initialize the value of maxSize.</p>
  
  <p>Print the message “Stack is full” in the push function when an attempt is made to push a data into a full stack.</p>
  
  <p>Print the message “Stack is empty” in the pop function and return the value -1000 when an attempt is made to pop data from an empty stack.</p>
  
  <p>Note that the statement “The contents of the stack are” is in the main function. In the display function, if the stack is empty, print “ {}”.</p>
  
  <p>Problem with my code:</p>
  
  <ul>
  <li>It's not taking the required amount of values.</li>
  <li><p>And secondly i am not able to print the elements.</p>
  
  <pre><code> #include&lt;stdio.h&gt;
   #include&lt;stdlib.h&gt;
   struct stack
     {
       int * a;
       int top;
       int maxSize;
     };
  void initstack(struct stack * p, int maxSize);
  void push(struct stack * p, int item);
  void display(struct stack p);
  int pop(struct stack * p);
  void printMenu();
  
    int main()  
     {
       struct stack p;
       int data,ch, data1, m;
       printf("Enter the maximum size of the stack\n");
       scanf("%d",&amp;m);
       initstack(&amp;p,m);
       do
         {
           printMenu();
           printf("Enter your choice\n");
           scanf("%d",&amp;ch);
           switch(ch)
             {
                case 1:
                printf("Enter the element to be pushed\n");
                scanf("%d",&amp;data);
                push(&amp;p, data);
                break;
  
                case 2:
                data1 = pop(&amp;p);
                if(data1 != -1000)
                printf("The popped element is %d\n",data1);
                break;
  
                case 3:
                printf("The contents of the stack are");
                display(p);
                printf("\n");
                break;
  
                default:
                return 0;
             }
         }
      while(1);
      return 0;
    }
  
     void printMenu()
       {
        printf("Choice 1 : Push\n");
        printf("Choice 2 : Pop\n");
        printf("Choice 3 : Display\n");
        printf("Any other choice : Exit\n");
       }
  
      void initstack(struct stack * p, int maxSize) 
       {
         p-&gt;top=-1;
         p-&gt;maxSize=maxSize;
       }
  
     void push(struct stack * p, int item) 
       {
         if (p-&gt;top == p-&gt;maxSize-1) 
           {
            printf("Stack is full\n");
            return;
           }
      p-&gt;a = (int *)malloc(sizeof(int));
      p-&gt;top++;
      p-&gt;a[p-&gt;top] =item;
      p-&gt;maxSize--;
  
     }
  
      void display(struct stack p) 
       {
         struct stack *p1;
         p1=&amp;p;
         int a[30],n=0,i;
         for (i = p1-&gt;top ; i &gt;= 0; i--)
          {
            printf("\n%d", p1-&gt;a[i]);
  
          }
  
        }
  
       int pop(struct stack * p) 
        {
          int num;
          if(p-&gt;top == -1)
           {
             printf("Stack is empty\n");
             return -1000;
           }
        num = p-&gt;a[p-&gt;top];
        p-&gt;top--;
        return num;
      }
  </code></pre></li>
  </ul>