gpt4 book ai didi

c - 如何将字符串复制到 C 中的数组?

转载 作者:太空宇宙 更新时间:2023-11-04 02:53:01 26 4
gpt4 key购买 nike

我正在创建一个带有推送和弹出功能的简单堆栈。我正在尝试将一系列字符串推送到充当内存堆栈的数组中。但是,GDB 一直显示我没有将字符串正确复制到数组中。有人对我如何解决这个问题有想法吗?

/*************************************************************************
* 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;
}

最佳答案

pop() 函数总是返回相同的字符串:

char* pop(void)
{
int i = CAPACITY-1; // i is the same!
return s.strings[i--]; // the same pointer
}

我建议将其更改为以下内容:

char* pop(void)
{
if (s.size == 0) return NULL;
char *str = s.strings[--s.size];
s.strings[s.size] = NULL;
return str;
}

它避免您在堆栈中存储过时的指针。

关于c - 如何将字符串复制到 C 中的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20187461/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com