Arduino: cannot pass union struct as pointer ac I can with gcc compiler

我怕爱的太早我们不能终老 提交于 2019-12-11 12:32:38

问题


I'm trying to use structs with Arduino but cannot pass a struct pointer with a function call. Everything works fine when adding a main function and compiling for my computer with gcc but with the Arduino IDE I get errors.

The code I tried was:

typedef union
{
    struct
    {
        unsigned unit   :2;
        unsigned channel:2;
        unsigned status :1;
        unsigned group  :1;
        unsigned remote :26;
    };
    unsigned long data;
} Signal;

Signal signal;

void testPassingStruct(Signal *variable)
{
    variable->status = 1;
}

void setup()
{
    signal.status = 1;
    testPassingStruct(&signal);
}

void loop()
{
}

And the errors was:

structtest:2: error: variable or field ‘testPassingStruct’ declared void
structtest:2: error: ‘Signal’ was not declared in this scope
structtest:2: error: ‘variable’ was not declared in this scope

回答1:


You have several unnamed items. This happens with typedefs. You could try,

union sig_union
{
    struct
    {
        unsigned unit   :2;
        unsigned channel:2;
        unsigned status :1;
        unsigned group  :1;
        unsigned remote :26;
    } d;                       /* Note the name here. */
    unsigned long data;
} signal;
typedef union sig_union Signal;

Then in your code you need,

void testPassingStruct(Signal *variable)
{
    variable->d.status = 1;
}

You can also use defines for masks and bit fields. Often it makes control of what bit is where clearer and the compiler will generate similar code in both cases. For instance,

#define SIG_UNIT(d)    (d&0x3)
#define SIG_CHANNEL(d) (d&0xc>>2)
#define SIG_STATUS(d)  (d&0x10)
/* etc. */

Especially with system programming where the bits matter, this is better as I don't believe that the 'C' bit fields specify how they are placed. Most of all, I am sure it will compile.




回答2:


It is basically the union and the lack of a the name for the structure member (I've called it parts in the code below). Try this

union Signal
{
    struct
    {
        unsigned unit   :2;
        unsigned channel:2;
        unsigned status :1;
        unsigned group  :1;
        unsigned remote :26;
    } parts;
    unsigned long data;
};

union Signal signal;

void testPassingStruct(union Signal *variable)
{
    variable->parts.status = 1;
}

void setup()
{
    signal.parts.status = 1;
    testPassingStruct(&signal);
}

void loop()
{
}


来源:https://stackoverflow.com/questions/21055234/arduino-cannot-pass-union-struct-as-pointer-ac-i-can-with-gcc-compiler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!