Search code examples
cpointersstackpointer-to-pointer

C: How do I check if a bi-dimensional array is empty and how to delete an element in sai array?


I have declared the array in this way:

static char **stack;

I have a function to push elements into the array/stack.

int push(const char *s) {
  if (p >= stack + size)
    return 0;
  *p++ = (char *)s;
  return 1;
}

Where

static char **p;
static size_t size;
p = stack;
size = mem_size / sizeof(char *);

However, how do I check if the bi-dimensional array is empty? And how should I pop an element out of the stack?


Solution

  • you could do something like this:

    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    static char **stack;
    static char **p;
    static size_t size;
    
    int
    push (const char *s)
    {
      if (p >= stack + size)
        return 0;
      *p++ = (char *) s;
      return 1;
    }
    
    char *
    pop (void)
    {
      if (p == stack)
        {
          return 0;
        }
      char *s = *(--p);
      *p = 0;
      return s;
    }
    
    void
    print_stack (char **stack_to_print)
    {
      char **temp = stack_to_print;
      while (*temp != NULL)
        {
          printf ("%s \n", *temp);
          temp++;
        }
    }
    
    int
    main ()
    {
      unsigned mem_size = 5 * sizeof (char *);
      stack = calloc (1, mem_size + sizeof (char *));   // add 1 extra NULL ptr and clear mem
      p = stack;
      size = mem_size / sizeof (char *);
    
      for (int i = 0; i < size; i++)
        {
          char s[2];
          sprintf (s, "%d", i);
          char *s1 = strdup (s);    // allocate copy of s to put in stack
          printf ("pushing: %s\n", s1);
          push (s1);
        }
      printf ("printing stack after pushing: \n");
      print_stack (stack);
      for (char *s; s = pop ();)
        {
          printf ("popped: %s\n", s);
          free (s);         // free ptr allocated with strdup
        }
      printf ("printing stack after popping: \n");
      print_stack (stack);
    
      return 0;
    }
    

    to produce this output:

    pushing: 0                                                                                                                                                          
    pushing: 1                                                                                                                                                          
    pushing: 2                                                                                                                                                          
    pushing: 3                                                                                                                                                          
    pushing: 4                                                                                                                                                          
    printing stack after pushing:                                                                                                                                       
    0                                                                                                                                                                   
    1                                                                                                                                                                   
    2
    3
    4                                                                                                                                    
    popped: 4                                                                                                                                                           
    popped: 3                                                                                                                                                           
    popped: 2                                                                                                                                                           
    popped: 1                                                                                                                                                           
    popped: 0                                                                                                                                                           
    printing stack after popping: