如何从c中的void函数返回动态数组?

如何从c中的void函数返回动态数组?,c,arrays,pointers,dynamic-memory-allocation,C,Arrays,Pointers,Dynamic Memory Allocation,我想通过引用从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; p

我想通过引用从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]);
}
main()
{
国际**a;
xxx&a;
printf(“%d\n\n”,a[1]);
}
无效xxx(整数**a)
{
int i;
*a=(int*)malloc(5*4);
对于(i=0;i<5;i++)
a[i]=i;
printf(“%d\n\n”,a[1]);
}
我只想在“xxx”函数中分配动态数组,并通过引用main将其返回,而不是打印它或将其用于其他用途。提前感谢:)

编辑

#包括
#包括
#定义宏
#定义\u CRT\u安全\u无\u警告
无效xxx(整数**a);
内部主(空)
{
int*a;
xxx&a;
printf(“%d\n\n”,a[1]);
}
无效xxx(整数**a)
{
int i;
*a=malloc(5*sizeof(**a));
对于(i=0;i<5;i++)
a[i]=i;
printf(“%d\n\n”,a[1]);
}
main()
中,您需要一个指针,而不是指向指针的指针。改变

  int **a;

并且,在
xxx()
中,更改

 a[i] = i;

也就是说

  • 不要使用神奇的数字,重写你的malloc语句,就像

    *a = malloc(5 * sizeof(**a));
    
    要更加健壮。此外,对于静态计数,请使用
    #define

  • main()
    不是托管环境的有效签名。您至少需要使用
    intmain(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
      }
      

      好了,伙计们,让它起作用的是

      a[i] = i;
      

      3个小时来回答这么简单的问题。 非常感谢在座的各位。
      有人能解释为什么会出现问题吗?

      。你很接近,但代码仍然有很多地方出错。你应该认真听你的编译器,因为它会向你发出警告。如果没有,那么你需要提高警告级别(无论如何你都应该这样做)。不要使用“幻数”。要么定义它们(比如数组长度),要么使用sizeof(标准或更复杂的用户定义类型)。这是非常古老的代码:过时的
      main
      定义,没有函数原型。您使用的是circa 1990编译器吗?现代C编译器免费提供。而且您不使用
      #include
      文件。有没有理由不直接返回指针?尽管您希望返回某些内容,但它有什么用处使函数
      void
      ?K.I.S.S!仅当您已经返回其他内容时才使用指向存储的指针。我正在使用新的visual studio。我像你说的那样修改了代码,但仍然不起作用。@Jen你的编译器告诉你了什么?一些警告?顺便说一句,我已经更新了我的答案。这不是唯一的问题,另一个问题是
      int**a
      main
      中。但是
      (*a)[i]=i
      引入了必要的额外间接层次-并且是这篇评论中的第一点,删除了
      main
      @WeatherVane中的间接层次。在我在这里发布之前,我尝试将代码中的内容从**a更改为*a,但没有任何效果,我尝试了很多事情,但甚至没有想过要这样做(*a)[I]=I。老实说,我不明白为什么会这样,不过还是要谢谢你
      *a = malloc(5 * sizeof(**a));
      
      #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;