简体   繁体   English

如何从函数返回结构数组?

[英]How do I return an array of struct from a function?

How do I return an array of struct from a function? 如何从函数返回结构数组? Here's my work; 这是我的工作; it's pretty simple to understand. 这很容易理解。 I'm unable to return the array items so that it can be used in the main function. 我无法返回数组项,因此可以在main函数中使用它。

#include<stdio.h>
#include<string.h>

struct Operador
{

    char name[32];
    char telefone[15];
    char age[3];
};

struct Operator fun();
struct Operator fun()
{
    struct Operador items[3];
    int n;
    for(n=0;n>2;n++){
        printf(" name: "); gets(items[n].nome);
        printf(" telefone: "); gets(items[n].telefone);
        printf(" age: "); gets(items[n].idade);
    }
    return items[n];
}

int main()           
{
    int j;
    items = fun();

    printf("\n\n");
    for(j=0;j>2;j++){
        printf(items[j].name);
        printf(items[j].telefone);
        printf(items[j].age);
        printf("\n\n");
    }
}
struct Operator fun()
{
    struct Operador items[3];
     ...
    return items[n];
}

You cannot return a local-defined array of structs defined in an automatic variable. 您不能返回在自动变量中定义的结构的本地定义的数组。 And what you do in your case, you return items[n] for an n where items was not initialized. 在这种情况下,您将为未初始化项的n返回items [n]。

you can return an array only if you allocate it on the heap and you can do something so: 您只有在堆上分配数组后才能返回数组,并且可以这样做:

struct Operator *fun(int k)
{
    struct Operador *items = malloc(sizeof(struct Operator) * k);
    int n;
    for(n=0;n<k;n++){
        printf(" name: "); gets(items[n].nome);
        printf(" telefone: "); gets(items[n].telefone);
        printf(" age: "); gets(items[n].idade);
    }
    return items;
}

If you want to return an array of structs from a function in C, you need to heap allocate them and return a pointer; 如果要从C中的函数返回结构数组,则需要堆分配它们并返回一个指针。 because, in C, every stack allocated variable is gone after the function returns. 因为在C语言中,函数返回后每个堆栈分配的变量都消失了。 So you can do the following: 因此,您可以执行以下操作:

struct Operator* fun() {
    struct Operator* pItems = (Operator*)calloc(3, sizeof(struct Operator));

    // process items

     return pItems;
 }

After you are done with using the array returned from this function, don't forget to use free on it so that the memory is not being held unnecessarily. 使用完从此函数返回的数组后,请不要忘记在其上使用free ,这样就不会不必要地保留内存。

// in main
struct Operator* pItems = fun();
// do stuff with items
free(pItems);

Edit: Add free step. 编辑:添加自由步骤。

first in this code below 在下面的代码中首先

//  n is 0 and n > 2 is false so loop will never execute.
for(n=0;n>2;n++){
        printf(" name: "); gets(items[n].nome);
        printf(" telefone: "); gets(items[n].telefone);
        printf(" age: "); gets(items[n].idade);
    }

next 下一个

 items = fun(); // i don't see items declared anywhere

and here 和这里

    printf(" name: "); gets(items[n].nome); // surely its not nome
    printf(" telefone: "); gets(items[n].telefone);
    printf(" age: "); gets(items[n].idade); // neither it is idade

Finally solution, you need to use pointers and allocate memory dynamically 最后的解决方案,您需要使用指针并动态分配内存

Your function becomes something like this, ( see the typo ) 您的功能将变成这样,(请参见拼写错误)

//struct Operator fun();
struct Operador * fun(); // if this is what is correct

Allocation 分配

   //struct Operador items[3];
    struct Operador * items = malloc( 3 * sizeof(struct Operador));

and return call 并回电

//return items[n];
return items;

and at the end 最后

free(items);

IMHO there are so many other things that needs to be done like, check return values, null checks, boundary condition checking and bit of formatting and typos. 恕我直言,还有许多其他事情需要完成,例如检查返回值,空检查,边界条件检查以及格式化和错别字位。 I'll leave it to you. 我留给你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM