C:如何判断一个二维数组是否为空以及如何删除sai数组中的元素?

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

我是这样声明数组的:

static char **stack;

我有一个函数可以将元素推入 array/stack。

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

在哪里

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

但是,如何判断二维数组是否为空?我应该如何从堆栈中弹出一个元素?

你可以这样做:

#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;
}

生成此输出:

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: