【问题标题】:What am I doing wrong when trying to implement an array of strings?尝试实现字符串数组时我做错了什么?
【发布时间】:2013-05-27 18:53:16
【问题描述】:
typedef struct ArrayList
{
    // We will store an array of strings (i.e., an array of char arrays)
    char **array;

    // Size of list (i.e., number of elements that have been added to the array)
    int size;

    // Length of the array (i.e., the array's current maximum capacity)
    int capacity;

} ArrayList;

以下函数应该为包含结构的头文件支持的字符串数组动态分配内存(见上文):

void panic(char *s)
{
    fprintf(stderr, "%s", s);
    exit(1);
}

ArrayList *createArrayList(int length){
    ArrayList *n = malloc(sizeof(ArrayList));

    int initial = 0, i;

    if (length > DEFAULT_INIT_LEN)
    {
        n->array = (char **)malloc(length * sizeof(int*));
        n->capacity = length;

        for (i = 0; i< n->capacity; i++)
        {
            n->array[i] = NULL;
        }
    }
    else
    {
        n->array = (char **)malloc(DEFAULT_INIT_LEN * sizeof(int*));
        n->capacity = DEFAULT_INIT_LEN;

        for (i = 0; i< n->capacity; i++)
        {
            n->array[i] = NULL;
        }
    }

    if (n->array == NULL)
        panic("ERROR: out of memory in Mylist!\n");

    n->size = initial;

    printf("-> Created new ArrayList of size %d\n", n->capacity);
    return n;
}

然后我有另一个函数应该打印当前在由 createArrayList 函数创建的新分配数组中的所有字符串:

void printArrayList(ArrayList *list)
{
    int i;

    for(i=0; i<list->capacity; i++)
    {
        if (list->array[i] == NULL)
            printf("(empty list)\n");
        else
            printf("%s\n",list->array[i]);

    }
}

当我在 main 函数中实现 printArrayList 函数(如上)时,输出为:

-> Created ArrayList of size 10 
(empty list)
(empty list)
(empty list)
(empty list)
(empty list)
(empty list)
(empty list)
(empty list)
(empty list)
(empty list)
(empty list)

但是,如果我在 createArrayList 函数中插入 strcpy(n-&gt;array[1], "apple"); 作为测试二维数组保存字符串能力的方法,则输出为:

-> Created ArrayList of size 10 

...然后它崩溃了

所以我的问题是我做错了什么?我是否错误地为我的阵列分配内存?我想得到它,所以输出是:

-> Created ArrayList of size 10 
(empty list)
apple
(empty list)
(empty list)
(empty list)
(empty list)
(empty list)
(empty list)
(empty list)
(empty list)
(empty list)

【问题讨论】:

  • 不是主要错误,但您应该分配长度 * sizeof(char*),而不是 length * sizeof(int*)。

标签: c memory dynamic struct allocation


【解决方案1】:

除了为ArrayList 分配内存外,您还需要为每个字符串分配存储空间。如果你想设置数组的第二个元素,你可以使用类似

void insert_at(ArrayList* arraylist, const char* str, int index)
{
    arraylist->array[index] = malloc(strlen(str)+1);
    if (arraylist->array[index] != NULL) {
        strcpy(arraylist->array[index], str);
    }
}

然后这样称呼它

insert_at(n, 1, "apple");

顺便说一句,你的代码就像

n->array = (char **)malloc(length * sizeof(int*));

应该是

n->array = malloc(length * sizeof(char*));

(它是一个指向 char 而不是 int 的指针数组,您不应该在 C 中转换来自 malloc 的返回)

【讨论】:

  • 或者简单的arraylist->array[index] = strdup(str).
  • @jarmod 是,如果strdup 可用。 (它是 Posix 的一部分,但不是标准 C 的一部分)
  • 谢谢大佬,现在一切都清楚了。它不像我看到的 strcpy 语句那么简单。
  • @KarimElsheikh 很高兴为您提供帮助。如果您的问题得到解答,可以考虑accepting an answer
【解决方案2】:

3 个问题

1) 小:改变

n->array = (char **)malloc(length * sizeof(int*));

n->array = (char **)malloc(length * sizeof(char*));

2) 将if (n-&gt;array == NULL) 直接测试放在n-&gt;array = (char **)malloc(... 之后

n->array = (char **)malloc(DEFAULT_INIT_LEN * sizeof(int*));
if (n->array == NULL)
   panic("ERROR: out of memory in Mylist!\n");

3) 最重要的。如果不先为n-&gt;array[1] 分配内存,就不能strcpy(n-&gt;array[1], "apple");。类似的东西

n->array[1] = malloc(strlen("Fred")+1);
strcpy(n->array[1], "Fred");

【讨论】:

  • 感谢您的提示,我知道我必须为这两个维度动态分配?
  • 是的,1 用于指针数组,然后是 'length' 分配,每个字符串 1。很好,你把测试像if (p == NULL) ...
猜你喜欢
  • 1970-01-01
  • 2020-07-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-22
  • 1970-01-01
  • 1970-01-01
  • 2020-01-17
  • 1970-01-01
相关资源
最近更新 更多