在 C 中传递多维数组
我目前正在尝试学习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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要将“数组”的地址传递给函数。 也就是说,你需要 char ***。 这是因为您需要通过为其分配内存来更改数组的值。
编辑:只是为了使其更完整,在函数声明中,您需要有类似的内容
然后您需要使用
在函数 make 中调用它,分配内存
并相应地更改其他位置。
另外,正如 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
Then you need to call it using
Inside the function make, allocate memory with
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.
这是工作代码:
正如其他人发布的那样 - 存在未使用的大小,并且 strdup 自行分配内存,并且之后释放内存很好......
Here is the working code:
As the other have posted - there was unused size, and strdup allocates memory by itself, and it is nice to free the memory afterwards...
请参阅 PolyThinker 的评论,这绝对是正确的。
除了传递数组的方式之外,您还应该检查其他一些问题:
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:
您正在传递数组的当前值以作为副本(在堆栈上)。 当你在 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 = ...
size 已声明但未分配任何值(我想这应该发生在函数 make 中)。
size is declared but gets no value assigned (that should happen in function make, I suppose).