简体   繁体   English

我如何从一个函数返回一个结构,该结构包含一个带有正确数组的数组?

[英]How do I return a struct (from a function) containing an array with the correct elements in that array?

I'm making a program that returns a struct containing an array, but the elements in the array are completely wrong. 我正在编写一个返回包含数组的结构的程序,但是数组中的元素完全错误。 I keep searching for an answer on this site, Google, and even Bing and nothing. 我一直在这个网站,Google甚至Bing上搜索答案,但一无所获。 The best I can find are answers like this: 我能找到的最好的答案是这样的:

Functions can't return arrays in C. 函数无法在C中返回数组。
However, they can return structs. 但是,它们可以返回结构。 And structs can contain arrays... 结构可以包含数组...

from How to make an array return type from C function? 如何从C函数使数组返回类型?

Now, how do I fix this without the use of pointers? 现在,如何在不使用指针的情况下解决此问题?

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

struct Codes{
int as;
int a[];
};

struct Codes create(int as){
    int a[as];
    for(int j = 0;j<as;j++)
        a[j]=j+1;
    struct Codes c;
    c.as = as;
    c.a[c.as];
    for(int i=0; i<as; i++)
        c.a[i] = a[i];

    for(int i=0; i<as; i+=1)
        printf("%d \n", c.a[i]);

    return c;
}

int main(int argc, char **argv) {

    struct Codes cd;
    int as = 4;
    cd = create(as);

    for(int i=0; i<4; i+=1)
        printf("%d \n", cd.a[i]);

}

Actual output: 实际输出:

1 
2 
3 
4 
0 
0 
2 
-13120 

Expected output: 预期产量:

1 
2 
3 
4 
1
2
3
4

In your function, struct Codes create(int as) , the struct Codes c; 在您的函数中, struct Codes create(int as)struct Codes c; is allocated on the stuck, so the memory is no longer valid once the function returns... 分配在卡住的内存上,因此一旦函数返回,内存将不再有效...

...It is true that the core struct is copied in the return value... but the variable array length ca isn't part of the struct (it's a memory "trailer" or "footer") and isn't copied along with the return value. ...确实是将核心结构复制到返回值中...但是可变数组长度ca不是该结构的一部分(它是内存“ trailer”或“ footer”),并且不会一起复制与返回值。

Either: 或者:

  1. allocate the struct and pass it to a struct Codes create(struct Codes *dest, int as) function; 分配结构并将其传递给struct Codes create(struct Codes *dest, int as)函数; OR 要么

  2. make the struct array fixed in size struct Codes{ int as; int a[4]; }; 使结构数组的大小固定在struct Codes{ int as; int a[4]; }; struct Codes{ int as; int a[4]; };

Good luck. 祝好运。

struct s with flexible value are not meant to be manipulated by value, only by pointer. 具有灵活值的struct并不意味着只能通过指针来操作值。

You cannot return a struct with a flexible member by value, because C does not know how many items it needs to allocate to the return value, and how many bytes it needs to copy. 您不能通过值返回带有弹性成员的struct ,因为C不知道需要为返回值分配多少项,以及需要复制多少字节。

Allocate your struct in dynamic memory using malloc of sufficient size, copy your data into it, and return a pointer to the caller: 使用足够大小的malloc在动态内存中分配struct ,将数据复制到其中,并返回指向调用方的指针:

struct Codes *c = malloc(sizeof(struct Codes)+as*sizeof(int));
c->as = as;
for (int i = 0 ; i != as ; i++) {
    c->a[i] = i+1;
}
return c;

Change your function to return a pointer; 更改函数以返回指针; make sure the caller frees the result. 确保调用者释放结果。

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

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