如何从 c 中的 void 函数 return 动态数组?
How to return dynamic array from void function in c?
我想 return 通过 void 函数引用动态数组。
我已经搜索了 3 个小时的答案,找不到任何有用的东西。
这是我的简化代码:
main()
{
int **a;
xxx(&a);
printf("%d\n\n", a[1]);
}
void xxx(int **a)
{
int i;
*a = (int*)malloc(5 * 4);
for (i = 0; i < 5; i++)
a[i] = i;
printf("%d\n\n", a[1]);
}
我只想在 "xxx" 函数中分配动态数组并通过引用 main return 它,而不是我想打印它或将它用于其他用途。提前致谢:)
编辑
#include <stdio.h>
#include <stdlib.h>
#define MACROs
#define _CRT_SECURE_NO_WARNINGS
void xxx(int **a);
int main(void)
{
int *a;
xxx(&a);
printf("%d\n\n", a[1]);
}
void xxx(int **a)
{
int i;
*a = malloc(5 * sizeof(**a));
for (i = 0; i < 5; i++)
a[i] = i;
printf("%d\n\n", a[1]);
}
在你的main()
中,你需要有一个指针,而不是指向指针的指针。变化
int **a;
至
int *a;
然后,在 xxx()
内,更改
a[i] = i;
至
(*a)[i] = i;
也就是说
不要使用幻数,重写你的 malloc 语句,如
*a = malloc(5 * sizeof(**a));
更加稳健。此外,对于 static 计数,使用 #define
宏。
Please see this discussion on why not to cast the return value of malloc()
and family in C
..
main()
不是托管环境的有效签名。您至少需要使用 int main(void)
。
我修改了一些东西并添加了一些评论。
#include <stdio.h> // please inlcude relevant headers
#include <stdlib.h>
#define ELEM 5 // you can change the requirement with a single edit.
void xxx(int **a) // defined before called - otherwise declare a prototype
{
int i;
*a = malloc(ELEM * sizeof(int)); // do not use magic numbers, don't cast
if(*a == NULL) {
exit(1); // check memory allocation
}
for (i = 0; i < ELEM; i++) {
(*a)[i] = i; // index correctly
}
}
int main(void) // 21st century definition
{
int *a; // correct to single *
int i;
xxx(&a);
for (i = 0; i < ELEM; i++) { // show results afterwards
printf("%d ", a[i]);
}
printf("\n");
free(a); // for completeness
}
程序输出:
0 1 2 3 4
好的,伙计们,让它发挥作用的是
a[i] = i;
至
(*a)[i] = i;
如此简单的答案需要 3 小时。
在这里非常感谢大家。
有人可以解释为什么会出现这个问题吗?
我想 return 通过 void 函数引用动态数组。 我已经搜索了 3 个小时的答案,找不到任何有用的东西。 这是我的简化代码:
main()
{
int **a;
xxx(&a);
printf("%d\n\n", a[1]);
}
void xxx(int **a)
{
int i;
*a = (int*)malloc(5 * 4);
for (i = 0; i < 5; i++)
a[i] = i;
printf("%d\n\n", a[1]);
}
我只想在 "xxx" 函数中分配动态数组并通过引用 main return 它,而不是我想打印它或将它用于其他用途。提前致谢:)
编辑
#include <stdio.h>
#include <stdlib.h>
#define MACROs
#define _CRT_SECURE_NO_WARNINGS
void xxx(int **a);
int main(void)
{
int *a;
xxx(&a);
printf("%d\n\n", a[1]);
}
void xxx(int **a)
{
int i;
*a = malloc(5 * sizeof(**a));
for (i = 0; i < 5; i++)
a[i] = i;
printf("%d\n\n", a[1]);
}
在你的main()
中,你需要有一个指针,而不是指向指针的指针。变化
int **a;
至
int *a;
然后,在 xxx()
内,更改
a[i] = i;
至
(*a)[i] = i;
也就是说
不要使用幻数,重写你的 malloc 语句,如
*a = malloc(5 * sizeof(**a));
更加稳健。此外,对于 static 计数,使用
#define
宏。Please see this discussion on why not to cast the return value of
malloc()
and family inC
..main()
不是托管环境的有效签名。您至少需要使用int main(void)
。
我修改了一些东西并添加了一些评论。
#include <stdio.h> // please inlcude relevant headers
#include <stdlib.h>
#define ELEM 5 // you can change the requirement with a single edit.
void xxx(int **a) // defined before called - otherwise declare a prototype
{
int i;
*a = malloc(ELEM * sizeof(int)); // do not use magic numbers, don't cast
if(*a == NULL) {
exit(1); // check memory allocation
}
for (i = 0; i < ELEM; i++) {
(*a)[i] = i; // index correctly
}
}
int main(void) // 21st century definition
{
int *a; // correct to single *
int i;
xxx(&a);
for (i = 0; i < ELEM; i++) { // show results afterwards
printf("%d ", a[i]);
}
printf("\n");
free(a); // for completeness
}
程序输出:
0 1 2 3 4
好的,伙计们,让它发挥作用的是
a[i] = i;
至
(*a)[i] = i;
如此简单的答案需要 3 小时。 在这里非常感谢大家。 有人可以解释为什么会出现这个问题吗?