在 C 中传递多维数组

发布于 2024-07-11 14:20:29 字数 745 浏览 5 评论 0原文

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

考虑一下:

#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 am currently trying to learn C and I have come to a problem that I've been unable to solve.

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,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

甜心 2024-07-18 14:20:29

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

编辑:只是为了使其更完整,在函数声明中,您需要有类似的内容

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

然后您需要使用

make(&array, &size);

在函数 make 中调用它,分配内存

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

并相应地更改其他位置。

另外,正如 kauppi 指出的那样,strdup 将为您分配内存,因此您不需要对每个字符串进行 malloc。

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.

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

*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.

白云悠悠 2024-07-18 14:20:29

这是工作代码:

#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;
}

正如其他人发布的那样 - 存在未使用的大小,并且 strdup 自行分配内存,并且之后释放内存很好......

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...

洒一地阳光 2024-07-18 14:20:29

请参阅 PolyThinker 的评论,这绝对是正确的。

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

  1. 也许您应该在 make(...) 中为 array_size 分配一些内容?
  2. strdup(char*) 分配内存,array[i] 的 malloc 不是必需的。
  3. 您应该在不再需要后释放所有分配的内存。

See PolyThinker's comment which is absolutely spot on.

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. strdup(char*) allocates memory, the malloc for array[i] is not necessary.
  3. You should free all the memory you allocate after you don't need it anymore.
飘逸的'云 2024-07-18 14:20:29

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

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 = ...

蓝天 2024-07-18 14:20:29

size 已声明但未分配任何值(我想这应该发生在函数 make 中)。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文