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 voidSo 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
Strucis used in the declaration of
funcbefore
Strucis known to the C compiler.[/code]Solution: Move the definition of
Strucinto 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
相关文章推荐
- use function pointer as parameter
- C++ AMP: uses tile_static(lds) as function param: 2 methods ( array or Pointer)
- linux sh : user array as function's input or output parameter
- C++: Function as template parameter, example
- [Swift] function pointer(address) as the parameter of function
- java call so :only use one struct as input and output parameter
- How to Use a Function or a Procedure as a Parameter in another Function
- !!!argc and argv as parameter of main function
- struct contains function pointer in c (zz)
- ExcelApplication.WorkBook.SaveAs function
- WSAIoctl函数SIO_GET_EXTENSION_FUNCTION_POINTER参数的意义
- Object in Java same as pointer
- looks like a function definition, but there is no parameter list; skipping apparent body
- pointer to a function used in arithmetic
- Programs as Data: Function Pointers
- json_parser_read.hpp:257:264: error: ‘type name’ declared as function returning an array escape
- function pointer(函数指针)
- Function Pointer of class member function (类的成员函数指针)
- 【技术】C语言传递指针到函数 About transfer pointer into function
- 容器技术未来可能的走势—Function As a Service