在C中传递多维数组

时间:2021-11-19 21:33:37

I am currently trying to learn C and I have come to a problem that I've been unable to solve.

我目前正在努力学习C,我遇到了一个我无法解决的问题。

Consider:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define ELEMENTS 5

void make(char **array, int *array_size) {
    int i;
    char *t = "Hello, World!";

    array = malloc(ELEMENTS * sizeof(char *));

    for (i = 0; i < ELEMENTS; ++i) {
        array[i] = malloc(strlen(t) + 1 * sizeof(char));
        array[i] = strdup(t);
    }
}

int main(int argc, char **argv) {
    char **array;
    int size;
    int i;

    make(array, &size);

    for (i = 0; i < size; ++i) {
        printf("%s\n", array[i]);
    }

    return 0;
}

I have no idea why the above fails to read back the contents of the array after creating it. I have literally spent an hour trying to understand why it fails but have come up empty handed. No doubt it's something trivial.

我不知道为什么上面的内容在创建之后无法回读数组的内容。我花了一个小时的时间试图理解它为什么失败但空手而归。毫无疑问,这是微不足道的。

Cheers,

5 个解决方案

#1


Here is the working code:

这是工作代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define ELEMENTS 5

void make(char ***array) {
    char *t = "Hello, World!";

    *array = malloc(ELEMENTS * sizeof(char *));

    int i;
    for (i = 0; i < ELEMENTS; ++i) {
        (*array)[i] = strdup(t);
    }
}

int main(int argc, char **argv) {
    char **array;
    make(&array);

    int i;
    for (i = 0; i < ELEMENTS; ++i) {
        printf("%s\n", array[i]);
        free(array[i]);
    }
    free(array);
    return 0;
}

As the other have posted - there was unused size, and strdup allocates memory by itself, and it is nice to free the memory afterwards...

正如另一个发布的那样 - 有未使用的大小,strdup自己分配内存,之后释放内存很好...

#2


You need to pass the address of "array" into the function. That is, you need char ***. This is because you need to change the value of array, by allocating memory to it.

您需要将“array”的地址传递给函数。也就是说,你需要char ***。这是因为您需要通过为其分配内存来更改数组的值。

EDIT: Just to make it more complete, in the function declaration you need to have something like

编辑:只是为了使它更完整,在函数声明中你需要有类似的东西

void make(char ***array, int *array_size)

Then you need to call it using

然后你需要使用它来调用它

make(&array, &size);

Inside the function make, allocate memory with

在函数make里面,配置内存

*array = malloc(ELEMENTS * sizeof(char *));

And change other places accordingly.

并相应地改变其他地方。

Also, as kauppi has pointed out, strdup will allocate memory for you, so you don't need to do malloc on each string.

另外,正如kauppi所指出的,strdup会为你分配内存,所以你不需要在每个字符串上做malloc。

#3


See PolyThinker's comment which is absolutely spot on.

请参阅PolyThinker的评论,这是绝对的观点。

In addition to the way you pass the array, you should check a few other issues:

除了传递数组的方式之外,还应该检查一些其他问题:

  1. Perhaps you should assign something to array_size in make(...)?
  2. 也许你应该在make(...)中为array_size分配一些东西?

  3. strdup(char*) allocates memory, the malloc for array[i] is not necessary.
  4. strdup(char *)分配内存,array [i]的malloc不是必需的。

  5. You should free all the memory you allocate after you don't need it anymore.
  6. 在您不再需要它之后,您应该释放所分配的所有内存。

#4


You are passing the current value of array to make as a copy (on the stack). when you change array in make(), you're only changing the copy, not the actual variable. Try passing by reference with &, or make it a char *** and work with *array = ...

您正在传递数组的当前值以作为副本(在堆栈上)。当您在make()中更改数组时,您只是更改副本,而不是实际变量。尝试用&传递引用,或者使它成为char ***并使用* array = ...

#5


size is declared but gets no value assigned (that should happen in function make, I suppose).

声明大小,但没有赋值(这应该发生在函数make中,我想)。

#1


Here is the working code:

这是工作代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define ELEMENTS 5

void make(char ***array) {
    char *t = "Hello, World!";

    *array = malloc(ELEMENTS * sizeof(char *));

    int i;
    for (i = 0; i < ELEMENTS; ++i) {
        (*array)[i] = strdup(t);
    }
}

int main(int argc, char **argv) {
    char **array;
    make(&array);

    int i;
    for (i = 0; i < ELEMENTS; ++i) {
        printf("%s\n", array[i]);
        free(array[i]);
    }
    free(array);
    return 0;
}

As the other have posted - there was unused size, and strdup allocates memory by itself, and it is nice to free the memory afterwards...

正如另一个发布的那样 - 有未使用的大小,strdup自己分配内存,之后释放内存很好...

#2


You need to pass the address of "array" into the function. That is, you need char ***. This is because you need to change the value of array, by allocating memory to it.

您需要将“array”的地址传递给函数。也就是说,你需要char ***。这是因为您需要通过为其分配内存来更改数组的值。

EDIT: Just to make it more complete, in the function declaration you need to have something like

编辑:只是为了使它更完整,在函数声明中你需要有类似的东西

void make(char ***array, int *array_size)

Then you need to call it using

然后你需要使用它来调用它

make(&array, &size);

Inside the function make, allocate memory with

在函数make里面,配置内存

*array = malloc(ELEMENTS * sizeof(char *));

And change other places accordingly.

并相应地改变其他地方。

Also, as kauppi has pointed out, strdup will allocate memory for you, so you don't need to do malloc on each string.

另外,正如kauppi所指出的,strdup会为你分配内存,所以你不需要在每个字符串上做malloc。

#3


See PolyThinker's comment which is absolutely spot on.

请参阅PolyThinker的评论,这是绝对的观点。

In addition to the way you pass the array, you should check a few other issues:

除了传递数组的方式之外,还应该检查一些其他问题:

  1. Perhaps you should assign something to array_size in make(...)?
  2. 也许你应该在make(...)中为array_size分配一些东西?

  3. strdup(char*) allocates memory, the malloc for array[i] is not necessary.
  4. strdup(char *)分配内存,array [i]的malloc不是必需的。

  5. You should free all the memory you allocate after you don't need it anymore.
  6. 在您不再需要它之后,您应该释放所分配的所有内存。

#4


You are passing the current value of array to make as a copy (on the stack). when you change array in make(), you're only changing the copy, not the actual variable. Try passing by reference with &, or make it a char *** and work with *array = ...

您正在传递数组的当前值以作为副本(在堆栈上)。当您在make()中更改数组时,您只是更改副本,而不是实际变量。尝试用&传递引用,或者使它成为char ***并使用* array = ...

#5


size is declared but gets no value assigned (that should happen in function make, I suppose).

声明大小,但没有赋值(这应该发生在函数make中,我想)。