tirithen
tirithen

Reputation: 3517

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

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

Upvotes: 1

Views: 1095

Answers (2)

cup
cup

Reputation: 8257

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()
{
}

Upvotes: 3

artless-noise-bye-due2AI
artless-noise-bye-due2AI

Reputation: 22430

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.

Upvotes: 3

Related Questions