0

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?

  • Possibly unrelated: you may like section 6 of the [comp.lang.c faq](http://c-faq.com/) – pmg Aug 25 '20 at 20:18

1 Answers1

0

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:   
Bill Morgan
  • 538
  • 2
  • 14
  • Thanks for the answer, it indeed works ! However, considering this method: ``` void print_stack(char **stack_to_print) { char **temp = stack_to_print; while (*temp != NULL) { printf("%s \n", *temp); temp++; } } ``` after using the method push I obtain a print of exactly what I have pushed. However, after having called pop() one or more times, the method to print still prints the same value as nothing was pop out of the stack. Is the method to print wrong or I did not understand a part of the code? – bugs_and_stars Aug 26 '20 at 06:11
  • updated the answer to add your print_stack function. The way you wrote the print_stack assumes the stack items are zeroed out with NULL and that you have at least 1 NULL so I added an extra to the initial stack allocation. Also updated pop to zero out the element in the stack and return a copy of the pointer. The code is here also: https://onlinegdb.com/rkAgx7NQw – Bill Morgan Aug 26 '20 at 17:47