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

前端 未结 3 411
滥情空心
滥情空心 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

提交回复
热议问题