Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 是否可以在函数内部分配数组并使用引用返回它?_C_Arrays_Pointers_Allocation - Fatal编程技术网

C 是否可以在函数内部分配数组并使用引用返回它?

C 是否可以在函数内部分配数组并使用引用返回它?,c,arrays,pointers,allocation,C,Arrays,Pointers,Allocation,我尝试过使用三重指针,但一直失败。代码: #include <stdlib.h> #include <stdio.h> int set(int *** list) { int count, i; printf("Enter number:\n"); scanf("%d", &count); (*list) = (int **) malloc ( sizeof (int) * count); for ( i = 0; i<count;i

我尝试过使用三重指针,但一直失败。代码:

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

int set(int *** list) {
  int count, i;
  printf("Enter number:\n");
  scanf("%d", &count);
  (*list) = (int **) malloc ( sizeof (int) * count);

  for ( i = 0; i<count;i++ ) {
    (**list)[count] = 123;
  }
  return count;
}

int main ( int argc, char ** argv )
{
  int ** list;
  int count;

  count = set(&list);

  return 0;
}
#包括
#包括
整数集(整数***列表){
int计数,i;
printf(“输入编号:\n”);
scanf(“%d”、&count);
(*list)=(int**)malloc(sizeof(int)*count);

对于(i=0;i您有一个整数数组数组。让我们仔细看看您的set函数:

for (i = 0; i < count;i++ ) {
    (**list)[count] = 123;
}

您所调用的列表实际上是一个数组。您可以通过以下方式执行此操作:

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

ssize_t set(int ** ppList) 
{
  ssize_t count = -1;

  printf("Enter number:\n");
  scanf("%zd", &count);

  if (0 <= count)
  {
    (*ppList) = malloc(count * sizeof **ppList);

    if (*ppList)
    {
      size_t i = 0;
      for (; i < count; ++i)
      {
        (*ppList)[i] = 42;
      }
    }
    else
    {
      count = -1;
    }
  }

  return count;
}

int main (void)
{
  int * pList = NULL;
  size_t count = 0;

  {
    ssize_t result = set(&pList);

    if (0 > result)
    {
      perror("set() failed");
    }
    else
    {
      count = result;
    }
  }

  if (count)
  {
    /* use pList */
  }

  ...

  free(pList);

  return 0;
}
#包括
#包括
ssize\u t集合(int**ppList)
{
ssize_t count=-1;
printf(“输入编号:\n”);
扫描频率(“%zd”,&count);
如果(结果为0)
{
perror(“set()失败”);
}
其他的
{
计数=结果;
}
}
如果(计数)
{
/*使用pList*/
}
...
免费(pList);
返回0;
}

据我所知,您想返回一个在另一个函数中分配的数组:这是这个函数的简单版本

#include<stdio.h>
#include<stdlib.h>
int* set(int *list) {
  int count, i;
  printf("Enter number:\n");
  scanf("%d", &count);
  list = (int *) malloc ( sizeof (int) * count);

  for ( i = 0; i<count;i++ ) {
    list[i] = 123;
  }

  return list;
}

int main ( int argc, char ** argv )
{
  int *list;
  list = set(list);
  //Use whatever you want to do with that array 
  free(list); // don't forget to free
  return 0;
}
#包括
#包括
整数*集合(整数*列表){
int计数,i;
printf(“输入编号:\n”);
scanf(“%d”、&count);
列表=(int*)malloc(sizeof(int)*计数);

对于(i=0;我很久以前就为自己制定了一条规则,如果我的代码有
***
任何地方,我都必须重写它。我听说过这个规则,但在这种情况下,***将节省创建一个只调用一次的函数的时间。不,这并不意味着重新编码,它意味着重新设计数据结构,这样你就不需要所有这些间接层了。你应该uld使用
(**list)[i]=123
而不是
(**list)[count]=123
强制性的“三星级程序员”笑话:哦,我没有看到。但问题是:如何将一维数组进行四舍五入并使用引用返回它?@Grant by reference你是指
返回&数组
还是什么?使用引用我的意思是作为输出参数。@Grant for(I到n)
(**list)[I]=new int[每个数组的期望长度]
for(k到m)
(*列表)[k]=所需的_值
但我假设您这样做是为了学习c语法。因为使用Triple指针不是一种好的编程风格,因为语法非常混乱。关于主题:顺便说一下,我是这个论坛的新手,我想学习如何在注释中输入新行。我没有检查malloc是否失败
#include<stdio.h>
#include<stdlib.h>
int* set(int *list) {
  int count, i;
  printf("Enter number:\n");
  scanf("%d", &count);
  list = (int *) malloc ( sizeof (int) * count);

  for ( i = 0; i<count;i++ ) {
    list[i] = 123;
  }

  return list;
}

int main ( int argc, char ** argv )
{
  int *list;
  list = set(list);
  //Use whatever you want to do with that array 
  free(list); // don't forget to free
  return 0;
}