简体   繁体   English

C:如何为我的类型化结构分配值?

[英]C: how do I assign values to my typed structure?

Following jldupont's answer on defining variables with varying data, I defined the following: 遵循jldupont关于使用可变数据定义变量的答案 ,我定义了以下内容:

enum buffer_format
{ 
    FIFO_U8T = 0, 
    FIFO_U16T 
};

struct buffer_param
{
    enum buffer_format type;
    union
    {
        struct buffer_fifo_u8_t *fifo_u8;
        struct buffer_fifo_u16_t *fifo_u16;
    } is;
};

I then coded the following assignments: 然后,我对以下任务进行了编码:

struct buffer_param fifo_uartTx_param;
fifo_uartTx_param.is.fifo_u8 = &fifo_uartTx;
fifo_uartTx_param.type = FIFO_U8T;

However, I am met with some errors that complain that I can't simply reach into my buffer_param type struct to make these assignments. 但是,我遇到一些错误,这些错误抱怨我不能简单地进入我的buffer_param类型结构来进行这些分配。 How would I assign the pointer to the UART fifo and set its type? 如何将指针分配给UART fifo并设置其类型?

error: expected '=', ',', ';', 'asm' or '__attribute__' before '.' token
 fifo_uartTx_param.is.fifo_u8 = &fifo_uartTx;
                  ^
error: expected '=', ',', ';', 'asm' or '__attribute__' before '.' token
 fifo_uartTx_param.type = FIFO_U8T;
                  ^

You're getting an error because you're attempting to do assignments outside of a function. 由于尝试在函数外部进行赋值,因此出现错误。 Only definitions with an initializer are allowed. 仅允许使用初始化程序进行定义。

To initialize this variable, do this: 要初始化此变量,请执行以下操作:

struct buffer_param fifo_uartTx_param = 
    { .type = FIFO_U8T, .is= { .fifo_u8 = &fifo_uartTx } };

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

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