您的位置:首页 > 产品设计 > UI/UE

Arduino: struct pointer as function parameter

2015-01-08 13:25 591 查看
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 pointer to a struct as a function parameter?Code:
typedef struct
{ int a,b;
} Struc;
void func(Struc *p) {  }
void setup() {Struc s;func(&s);}void loop(){}
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 1typedef 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.[/code]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;};
The answer above works. In the meantime I had found the following also to work, without the need for a .h file:
typedef struct MyStruc{ int a,b;} Struc;void func(struct MyStruc *p) {  }void setup() {Struc s;func(&s);}
void loop(){}
Be warned: Arduino coding is a little flaky. Many of the libraries are also a bit flaky!see:http://stackoverflow.com/questions/17493354/arduino-struct-pointer-as-function-parameter
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: