返回变量结构成员 [英] Return variable struct members

查看:91
本文介绍了返回变量结构成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要返回一个带有两个值的结构.一个双精度值( time )和一个大小可变的uint8_t数组.我有两个函数,并且两个函数都应返回相同类型的结构,但具有不同的数据成员( data [9],data [64] ).

我已经尝试用其他成员 size 创建一个结构,但这根本不起作用. size 应该使用固定长度初始化数组,但是编译器说未定义变量大小.

  typedef struct结果{双倍时间;整数大小;uint8_t data [size];} 

以前的命令不起作用,因此我尝试创建一个空数组并在函数中对其进行初始化,但也没有起作用.

  typedef struct结果{双倍时间;uint8_t data [];} 结果;结果foo(){两倍时间= 17.5;uint8_t data [9] = {0};结果res = {sizeof(data),time,data};返回资源;}结果栏(){两倍时间= 9.5;uint8_t data [64] = {4};结果res = {sizeof(data),time,data};返回资源;}int main(void){结果foo = foo();printf(%.6f \ n",foo-> time);uint8_t data [9] = foo-> data;//使用data [9] ...结果bar = bar();printf(%.6f \ n",bar-> time);uint8_t data [64] = bar-> data;//使用数据[64] ...} 

我收到此错误消息:

 错误:返回类型是不完整的类型 

结构的成员应该可用,如main函数中所示.我认为编译器不知道 data 数组应该有多大,但是也许有人可以向我解释这个上下文以及我的问题,即如何返回其中包含可变大小数组的结构.

我将不胜感激,非常感谢.

解决方案

您需要使用灵活的数组成员(FAM):

  typedef struct结果{双倍时间;size_t大小;uint8_t data [];}; 

然后您可以使用 malloc()等分配一个内存块来保存数据:

  uint8_t data [] =这是可变长度的有效载荷";结构结果* rp = malloc(sizeof(结构结果)+ sizeof(数据));rp-> size = sizeof(data);strcpy(rp-> data,data);rp->时间= 17.5;返回rp; 

您可以安排不同数量的数据-调整大小并使用 memmove()(或 memcpy())进行复制.

I need to return a struct with two values in it. A double value (time) and an uint8_t array with a variable size. I have two functions and both of them should return the same type of struct, but with different data members (data[9], data[64]).

I've already tried to create a struct with an additional member size, but this isn't working at all. size should initialize the array with a fixed length, but the compilers says that the variable size is not defined.

typedef struct Result {
    double time;
    int size;
    uint8_t data[size];
}

The previous wasn't working so I tried to create an empty array and initialize it within my functions, but did not work either.

typedef struct Result {
    double time;
    uint8_t data[];
} Result;

Result foo() {
    double time = 17.5;
    uint8_t data[9] = {0};
    Result res = {sizeof(data), time, data};
    return res;
}

Result bar() {
    double time = 9.5;
    uint8_t data[64] = {4};
    Result res = {sizeof(data), time, data};
    return res;
}

int main(void) {
    Result foo = foo();
    printf("%.6f\n", foo->time);
    uint8_t data[9] = foo->data;
    // work with data[9] ...

    Result bar = bar();
    printf("%.6f\n", bar->time);
    uint8_t data[64] = bar->data;
    // work with data[64] ...
}

I get this error message:

Error: return type is an incomplete type

The members of the struct should be available as shown in the main function. I think the compiler doesn't know how big the data array should be, but maybe someone can explain me this context and my question on how to return a struct with a variable sized array in it.

I would appreciate any help, thank you very much.

解决方案

You need to use a flexible array member (FAM):

typedef struct Result {
    double  time;
    size_t  size;
    uint8_t data[];
};

You can then allocate a block of memory with malloc() et al to hold the data:

uint8_t data[] = "This is the variable length payload";
struct Result *rp = malloc(sizeof(struct Result) + sizeof(data));
rp->size = sizeof(data);
strcpy(rp->data, data);
rp->time = 17.5;

return rp;

You can arrange for different amounts of data — adjust the size and use memmove() (or memcpy()) to copy it.

这篇关于返回变量结构成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆