2

I'm having trouble solving an unknown problem, that I've never experienced on Arduino Nano(ATmega328). I'm building up a drone source code and it has been doing well. But, errors have suddenly appeared in Arduino IDE.
Obviously, I'm a rookie on programming. So help me finding out how to solve this problems.

I got errors like this:

error: expected unqualified-id before '[' token 

error: 'value' does not name a type

 Quadcopter_Code_Arduino:580: error: expected unqualified-id 
 before '[' token

  struct op[]

           ^

Quadcopter_Code_Arduino:589: error: 'value' does not name a type

   } value;

     ^
typedef union accel_t_gyro_union
{
  struct
  {
    uint8_t x_accel_h;
    uint8_t x_accel_l;
    uint8_t y_accel_h;
    uint8_t y_accel_l;
    uint8_t z_accel_h;
    uint8_t z_accel_l;
    uint8_t t_h;
    uint8_t t_l;
    uint8_t x_gyro_h;
    uint8_t x_gyro_l;
    uint8_t y_gyro_h;
    uint8_t y_gyro_l;
    uint8_t z_gyro_h;
    uint8_t z_gyro_l;
  } reg;
  struct op[]
  {
    int x_accel;
    int y_accel;
    int z_accel;
    int temperature;
    int x_gyro;
    int y_gyro;
    int z_gyro;
  } value;

};

gre_gor
  • 6,669
  • 9
  • 47
  • 52
Ms.K
  • 21
  • 3
  • You might also want to check this question [Expected unqualified-id before '[' token](http://stackoverflow.com/questions/28492647/expected-unqualified-id-before-token) – Bo Persson Mar 13 '17 at 11:22
  • 1
    Possible duplicate of [Expected unqualified-id before '\[' token](http://stackoverflow.com/questions/28492647/expected-unqualified-id-before-token) – m7913d Mar 13 '17 at 13:08

3 Answers3

2

The struct op[] { ... } looks weird to me. (I even don't know how to read/interprete this.)

Thus, I tested this with gcc on cygwin (in a simplified version):

$ gcc --version
gcc (GCC) 5.4.0

$ echo 'typedef union u { struct op[] { int i; } value; };
> int main() { return 0; } 
> ' > test-weird-union.c

$ gcc test-weird-union.c   
test-weird-union.c:1:28: error: expected identifier or '(' before '[' token
 typedef union u { struct op[] { int i; } value; };
                            ^
test-weird-union.c:1:42: error: expected ';' before 'value'
 typedef union u { struct op[] { int i; } value; };
                                          ^
test-weird-union.c:1:42: warning: useless storage class specifier in empty declaration

(Same output with gcc -std=c11.)

The code is probably wrong or at least not standard C.

OK. Thinking additionally about it: How to fix it?

A little bit more background would be nice. Thus, see this as an approach and prove how to apply it:

#include <stdint.h>

typedef union accel_t_gyro_union
{
  struct
  {
    uint8_t x_accel_h;
    uint8_t x_accel_l;
    uint8_t y_accel_h;
    uint8_t y_accel_l;
    uint8_t z_accel_h;
    uint8_t z_accel_l;
    uint8_t t_h;
    uint8_t t_l;
    uint8_t x_gyro_h;
    uint8_t x_gyro_l;
    uint8_t y_gyro_h;
    uint8_t y_gyro_l;
    uint8_t z_gyro_h;
    uint8_t z_gyro_l;
  } reg;
  struct op
  {
    int16_t x_accel;
    int16_t y_accel;
    int16_t z_accel;
    int16_t temperature;
    int16_t x_gyro;
    int16_t y_gyro;
    int16_t z_gyro;
  } value;
} accel_t_gyro_union;

The following things I have done:

  1. Removed the [] behind op to fix the syntax error.

  2. Changed the int to int16_t. This assures that this code works also on non-16 bit platforms.

  3. Added accel_t_gyro_union to the end of typedef. This might not be necessary but I felt comfortable with this.

Again checked with gcc on cygwin:

$ gcc -std=c11 test-weird-union.c

At least, no syntax errors. The rest is beyond my knowledge.

Scheff's Cat
  • 19,528
  • 6
  • 28
  • 56
1

Definition/Information:

From http://en.cppreference.com/w/cpp/language/union

A union is a special class type that can hold only one of its non-static data members at a time.

And

The union is only as big as necessary to hold its largest data member. The other data members are allocated in the same bytes as part of that largest member.

Also:

Your declaration is incorrect and should be:

struct op
{
    int x_accel;
    int y_accel;
    int z_accel;
    int temperature;
    int x_gyro;
    int y_gyro;
    int z_gyro;
} value [SIZE];

I advise you to use a specific size for your array, OR a pointer declartion of your struct type to make clear, that you will decide it's heap-size later, but the union on the stack will have a pointer-size for the member...

(Although with empty brackets it still compiles and works in my quick testing... I should read into this behaviour, too. :D)

Solution:

Use the corrected declaration above.

Advice: Use a SIZE for the array in the brackets. If you don't know the size yet, use a pointer for reasons mentioned above.

MABVT
  • 1,350
  • 10
  • 17
1

The struct op[] { ... } is nonsense anyway. If you want struct type named op, it should be outside of union (but it's not really issue, there might be a problem to use it somewhere else):

struct A {
    uint8_t  low_byte; // depends on byte order 
    uint8_t high_byte;
};

struct B {
    int          word;
};

union U {
    A  reg;
    B  val;
} variable;

But there is no need for the array at all. So the second struct should be also anonymous:

union accel_t_gyro_union
{
  struct
  {
    uint8_t x_accel_h;
    uint8_t x_accel_l;
    uint8_t y_accel_h;
    uint8_t y_accel_l;
    uint8_t z_accel_h;
    uint8_t z_accel_l;
    uint8_t t_h;
    uint8_t t_l;
    uint8_t x_gyro_h;
    uint8_t x_gyro_l;
    uint8_t y_gyro_h;
    uint8_t y_gyro_l;
    uint8_t z_gyro_h;
    uint8_t z_gyro_l;
  } reg;
  struct
  {
    int x_accel;
    int y_accel;
    int z_accel;
    int temperature;
    int x_gyro;
    int y_gyro;
    int z_gyro;
  } value;
};
KIIV
  • 3,534
  • 2
  • 18
  • 23