我正在创建一个具有推送和弹出功能的简单堆栈。我试图将一系列字符串推入一个充当内存堆栈的数组中。但是,GDB一直告诉我,我没有正确地将字符串复制到数组中。任何人有关于我如何解决这个问题的想法?如何将字符串复制到C中的数组?
/*************************************************************************
* stack.c
*
* Implements a simple stack structure for char* s.
************************************************************************/
// for strdup() in the testing code
#define _XOPEN_SOURCE 500
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// the capacity of the stack
#define CAPACITY 10
//global variable used to keep track of pop and push
typedef struct
{
// storage for the elements in the stack
char* strings[CAPACITY];
// the number of elements currently in the stack
int size;
} stack;
// declare a stack (as a global variable)
stack s;
/**
* Puts a new element into the stack onto the "top" of the data structure
* so that it will be retrived prior to the elements already in the stack.
*/
bool push(char* str)
{
int i = 0;
s.strings[i++] = strdup(str);
++s.size;
return false;
}
/**
* Retrieves ("pops") the last ("top") element off of the stack, following
* the "last-in, first-out" (LIFO) ordering of the data structure. Reduces
* the size of the stack.
*/
char* pop(void)
{
int i = CAPACITY-1;
return s.strings[i--];
}
/**
* Implements some simple test code for our stack
*/
int main(void)
{
// initialize the stack
s.size = 0;
printf("Pushing %d strings onto the stack...", CAPACITY);
for (int i = 0; i < CAPACITY; i++)
{
char str[12];
sprintf(str, "%d", i);
push(strdup(str));
}
printf("done!\n");
printf("Making sure that the stack size is indeed %d...", CAPACITY);
assert(s.size == CAPACITY);
printf("good!\n");
printf("Making sure that push() now returns false...");
assert(!push("too much!"));
printf("good!\n");
printf("Popping everything off of the stack...");
char* str_array[CAPACITY];
for (int i = 0; i < CAPACITY; i++)
{
str_array[i] = pop();
}
printf("done!\n");
printf("Making sure that pop() returned values in LIFO order...");
for (int i = 0; i < CAPACITY; i++)
{
char str[12];
sprintf(str, "%d", CAPACITY - i - 1);
assert(strcmp(str_array[i], str) == 0);
free(str_array[i]);
}
printf("good!\n");
printf("Making sure that the stack is now empty...");
assert(s.size == 0);
printf("good!\n");
printf("Making sure that pop() now returns NULL...");
assert(pop() == NULL);
printf("good!\n");
printf("\n********\nSuccess!\n********\n");
return 0;
}
以下两个答案都是正确运行所需的:-) – anishsane
同意,两者都需要改变。但是,弹出功能仍然给我分段错误。不知道为什么... – KishB87