如何从C中的函数返回1000个变量?

如何从C中的函数返回1000个变量?,c,C,如何从C语言中的函数返回1000个变量? 这是一个我无法回答的面试问题 我想在指针的帮助下,我们可以做到这一点。我不熟悉指针,C有谁能给我解决这个问题的方法,或者使用指针,或者使用不同的方法吗?将它们都打包到一个结构中并返回该结构 struct YourStructure { int a1; int b2; int z1000; }; YouStructure doSomething(); 将它们全部打包到一个结构中,然后返回该结构 struct YourStruc

如何从C语言中的函数返回1000个变量?
这是一个我无法回答的面试问题


我想在指针的帮助下,我们可以做到这一点。我不熟悉指针,C有谁能给我解决这个问题的方法,或者使用指针,或者使用不同的方法吗?

将它们都打包到一个结构中并返回该结构

struct YourStructure
{
    int a1;
    int b2;

    int z1000;
};

YouStructure doSomething();

将它们全部打包到一个结构中,然后返回该结构

struct YourStructure
{
    int a1;
    int b2;

    int z1000;
};

YouStructure doSomething();

如果是相同类型的1000倍(例如int):

outArr
持有的内存在使用后必须释放:

free(outArr);
查看它在ideone上运行:


替代解决方案:不要让
myfunc
为整数数组分配内存,而是让调用者完成工作并将数组大小传递给函数:

void myfunc2(int* out, int len){
  int i = 0;
  for(i = 0; i < len; i++){
    out[i] = i;
  }
}
同样,调用方必须释放
outArr
的内存


第三种方法:静态内存。使用静态内存调用myfunc2:

int outArr[1000];
myfunc2(outArr, 1000);

在这种情况下,不需要分配或释放内存。

如果是相同类型的1000倍(例如int):

outArr
持有的内存在使用后必须释放:

free(outArr);
查看它在ideone上运行:


替代解决方案:不要让
myfunc
为整数数组分配内存,而是让调用者完成工作并将数组大小传递给函数:

void myfunc2(int* out, int len){
  int i = 0;
  for(i = 0; i < len; i++){
    out[i] = i;
  }
}
同样,调用方必须释放
outArr
的内存


第三种方法:静态内存。使用静态内存调用myfunc2:

int outArr[1000];
myfunc2(outArr, 1000);

在这种情况下,无需分配或释放内存。

数组指针方法:

int * output(int input)
{
    int *temp=malloc(sizeof(int)*1000);
    // do your work with 1000 integers
    //...
    //...
    //...
    //ok. finished work with these integers
    return temp;
}
struct my_struct
{
    int a;
    int b;
    double x;
    ...
    //1000 different things here
    struct another_struct;
}parameter;


my_struct * output(my_struct what_ever_input_is)
{
    my_struct *temp=malloc(sizeof(my_struct));
    //...
    //...
    return temp;
}
结构指针方法:

int * output(int input)
{
    int *temp=malloc(sizeof(int)*1000);
    // do your work with 1000 integers
    //...
    //...
    //...
    //ok. finished work with these integers
    return temp;
}
struct my_struct
{
    int a;
    int b;
    double x;
    ...
    //1000 different things here
    struct another_struct;
}parameter;


my_struct * output(my_struct what_ever_input_is)
{
    my_struct *temp=malloc(sizeof(my_struct));
    //...
    //...
    return temp;
}

数组指针方法:

int * output(int input)
{
    int *temp=malloc(sizeof(int)*1000);
    // do your work with 1000 integers
    //...
    //...
    //...
    //ok. finished work with these integers
    return temp;
}
struct my_struct
{
    int a;
    int b;
    double x;
    ...
    //1000 different things here
    struct another_struct;
}parameter;


my_struct * output(my_struct what_ever_input_is)
{
    my_struct *temp=malloc(sizeof(my_struct));
    //...
    //...
    return temp;
}
结构指针方法:

int * output(int input)
{
    int *temp=malloc(sizeof(int)*1000);
    // do your work with 1000 integers
    //...
    //...
    //...
    //ok. finished work with these integers
    return temp;
}
struct my_struct
{
    int a;
    int b;
    double x;
    ...
    //1000 different things here
    struct another_struct;
}parameter;


my_struct * output(my_struct what_ever_input_is)
{
    my_struct *temp=malloc(sizeof(my_struct));
    //...
    //...
    return temp;
}

这就是你在C语言中所做的

void func (Type* ptr);
/*
   Function documentation.
   Bla bla bla...

   Parameters
   ptr      Points to a variable of 'Type' allocated by the caller. 
            It will contain the result of...

*/
如果你的目的不是通过“ptr”返回任何东西,你会写信的

void func (const Type* ptr);

相反。

这是在C中实现的方法

void func (Type* ptr);
/*
   Function documentation.
   Bla bla bla...

   Parameters
   ptr      Points to a variable of 'Type' allocated by the caller. 
            It will contain the result of...

*/
如果你的目的不是通过“ptr”返回任何东西,你会写信的

void func (const Type* ptr);


相反。

1000个变量或1000个相同类型的值?当面嘲笑他们。如果他们是认真的,那就快跑。@cnicutar-hahahahah,+1,你说得对。@KirilKirov我不知道他们在这里用
变量是什么意思,我猜是variables。1000个变量或1000个相同类型的值?当着他们的面笑吧。如果他们是认真的,运行。@ cNICUTAR -哈哈哈,1,你是对的。“Kielkrv我不知道这里的意思是“代码>变量< /Calp>,我猜变量。一个名字不能从C和C++中的数字开始:)一个名字不能从C和C++中的数字开始):也就是内存泄漏的方法。让调用者分配内存,并将指针作为参数传递。你是对的。但这是另一种方式。这种方式是存在的。这只是一个访谈。如果可能的话,这将是防止这种事情发生的另一个原因。但是如果malloc()的指针被释放,那么它就不再是泄漏了是的,但是函数与内存分配无关,但是这样做,期望其他人来清理他们的混乱,是10个中有9个中的内存泄漏源。我意识到这只是一个“快速脏”的例子,但为什么一个名为“输出”的函数要分配动态内存并将其返回给调用者呢?这样的程序设计毫无意义。还有内存泄漏方法。让调用者分配内存,并将指针作为参数传递。你是对的。但这是另一种方式。这种方式是存在的。这只是一个访谈。如果可能的话,这将是防止这种事情发生的另一个原因。但是如果malloc()的指针被释放,那么它就不再是泄漏了是的,但是函数与内存分配无关,但是这样做,期望其他人来清理他们的混乱,是10个中有9个中的内存泄漏源。我意识到这只是一个“快速脏”的例子,但为什么一个名为“输出”的函数要分配动态内存并将其返回给调用者呢?这样的程序设计毫无意义。请解释一下
返回的目的填充。如果你能给出一个合理的解释,我会投赞成票。@Lundin:你说得对。不需要返回
。编辑答案。请解释返回的目的填充。如果你能给出一个合理的解释,我会投赞成票。@Lundin:你说得对。不需要返回
。编辑答案。