gpt4 book ai didi

c - 在 C 中编写/分配字符串列表

转载 作者:行者123 更新时间:2023-11-30 16:54:36 24 4
gpt4 key购买 nike

我正在用 C 语言创建一个字符串列表,但我在网上找不到任何可以轻松添加然后清除字符串列表的内容。这与 Perl 的“推送”功能很接近。

我想在这里分享我想出的代码:

#include <stdio.h>//printf
#include <stdlib.h>//EXIT_FAILURE, malloc, realloc
#include <string.h>//need for strcmp and strlen

void *add_name(char **restrict array, size_t *restrict size, size_t *restrict number_of_names, const char *restrict NAME) {//this function works, when freed inside main()
for (unsigned int name = *number_of_names-1; name <= 0; name--) {
if (strcmp(array[name], NAME) == 0) {
return array;//if this name is already in the list, don't add it
}
}
const size_t NAME_SIZE = strlen(NAME)*sizeof(char)+sizeof(char*);
*size += NAME_SIZE;
array = realloc(array, *size);
if (array == NULL) {
puts("realloc of array failed.\n");
exit(EXIT_FAILURE);
}
array[*number_of_names] = malloc(NAME_SIZE);
if (array[*number_of_names] == NULL) {
printf("alloc of array[%zu] failed.\n", *number_of_names);
exit(EXIT_FAILURE);
}
strcpy(array[*number_of_names], NAME);
*number_of_names += 1;
return array;
}

void *clear_names(char **restrict array, size_t *restrict size, size_t *restrict number_of_names) {//empty the list of names
for (size_t name = 0; name < *number_of_names; name++) {
free(array[name]); array[name] = NULL;//this is missing something
if (array[name] != NULL) {
printf("free of array[%zu] failed.\n", name);
}
}
*number_of_names = 0;
*size = sizeof(char);
array = realloc(array, *size);
if (array == NULL) {
printf("realloc of array failed @ line %u\n",__LINE__);
exit(EXIT_FAILURE);
}
return array;
}

int main (void) {
puts("This program demonstrates how to add to and clear a list of names.");
size_t name_list_size = sizeof(char);
char **name_list = malloc(name_list_size);
size_t number_of_names = 0;
name_list = add_name(name_list, &name_list_size, &number_of_names, "James");
name_list = add_name(name_list, &name_list_size, &number_of_names, "Mary");
name_list = add_name(name_list, &name_list_size, &number_of_names, "Mary");
name_list = add_name(name_list, &name_list_size, &number_of_names, "Thomas");
for (size_t name = 0; name < number_of_names; name++) {
printf("%s\n",name_list[name]);
}
name_list = clear_names(name_list, &name_list_size, &number_of_names);
//
name_list = add_name(name_list, &name_list_size, &number_of_names, "Matthew");
name_list = add_name(name_list, &name_list_size, &number_of_names, "Mark");
name_list = add_name(name_list, &name_list_size, &number_of_names, "Luke");
name_list = add_name(name_list, &name_list_size, &number_of_names, "John");
for (size_t name = 0; name < number_of_names; name++) {
printf("%s\n",name_list[name]);
}
//
name_list = clear_names(name_list, &name_list_size, &number_of_names);
name_list = add_name(name_list, &name_list_size, &number_of_names, "Titus");
printf("size = %zu number_of_names = %zu\n", name_list_size, number_of_names);
name_list = add_name(name_list, &name_list_size, &number_of_names, "Peter");
printf("size = %zu number_of_names = %zu\n", name_list_size, number_of_names);
name_list = add_name(name_list, &name_list_size, &number_of_names, "Bartholomew");
printf("size = %zu number_of_names = %zu\n", name_list_size, number_of_names);
name_list = add_name(name_list, &name_list_size, &number_of_names, "Paul");
printf("size = %zu number_of_names = %zu\n", name_list_size, number_of_names);
for (size_t name = 0; name < number_of_names; name++) {
printf("%s\n",name_list[name]);
}
name_list = clear_names(name_list, &name_list_size, &number_of_names);
free(name_list); name_list = NULL;
return 0;
}

最佳答案

我提出以下建议:

  • realloc分配给临时指针,因此如果它返回NULL,则不会破坏数组
  • 使用 strdup 而不是 malloc 进行字符串存储
  • 使用lsearch检查并插入字符串
  • 当增加数组的大小时,通过加倍而不是线性来实现
  • 将数组及其数量和容量打包在 struct 中(见下文),并将其作为参数传递给 add 和clear 函数

您需要添加一个比较函数以传递给lsearch。如果 lsearch 添加名称,它会更新计数。分配仍然取决于您,因此在调用之前请确保容量超过计数。

如果你这样做,我想你会发现你的代码更小更简单。它还有助于添加删除和搜索功能。

使用该结构的一个优点是您可以使用较短的名称,例如:

struct name_list_t { 
char **array;
size_t n, capacity; } names = { .array = NULL };

//this function works, when freed inside main()

嗯。该评论实际上没有意义,因为函数没有被释放。我明白你的意思,但你让我工作。

HTH。

关于c - 在 C 中编写/分配字符串列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40510714/

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