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

前端 未结 3 389
滥情空心
滥情空心 2020-12-07 03:54

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

相关标签:
3条回答
  • 2020-12-07 04:36
    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.

    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;
    }
    
    0 讨论(0)
  • 2020-12-07 04:36

    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.

    0 讨论(0)
  • 2020-12-07 04:42

    If you want to return an array of structs from a function in C, you need to heap allocate them and return a pointer; because, in C, every stack allocated variable is gone after the function returns. 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.

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

    Edit: Add free step.

    0 讨论(0)
提交回复
热议问题