Arduino: struct pointer as function parameter

后端 未结 4 1246
我在风中等你
我在风中等你 2020-12-21 16:45

The code below gives the error:

sketch_jul05a:2: error: variable or field \'func\' declared void

So my question is: how can I pass a point

4条回答
  •  萌比男神i
    2020-12-21 17:13

    The problem is, that the Arduino-IDE auto-translates this into C like this:

    #line 1 "sketch_jul05a.ino"
    #include "Arduino.h"
    void func(Struc *p);
    void setup();
    void loop();
    #line 1
    typedef struct
    { int a,b;
    } Struc;
    
    
    void func(Struc *p) {  }
    
    void setup() {
      Struc s;
      func(&s);
    }
    
    void loop()
    {
    }
    

    Which means Struc is used in the declaration of func before Struc is known to the C compiler.

    Solution: Move the definition of Struc into another header file and include this.

    Main sketch:

    #include "datastructures.h"
    
    void func(Struc *p) {  }
    
    void setup() {
      Struc s;
      func(&s);
    }
    
    void loop()
    {
    }
    

    and datastructures.h:

    struct Struc
    { int a,b;
    };
    

提交回复
热议问题