Arduino的:结构指针作为函数参数 [英] Arduino: struct pointer as function parameter
本文介绍了Arduino的:结构指针作为函数参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在code以下给出了错误:
sketch_jul05a:2:错误:变量或字段'功能'宣布无效
所以我的问题是:我怎么能传递一个指针结构作为函数参数
?code:
typedef结构
{诠释A,B;
} STRUC;
无效FUNC(STRUC * P){}无效设置(){
STRUC S;
FUNC(安培; S);
}无效循环()
{
}
解决方案
的问题是,是,Arduino的IDE自动转换为C这是这样的:
的#line 1sketch_jul05a.ino
#包括Arduino.h
无效FUNC(STRUC * P);
无效设置();
空隙环();
1的#line
typedef结构
{诠释A,B;
} STRUC;
无效FUNC(STRUC * P){}无效设置(){
STRUC S;
FUNC(安培; S);
}无效循环()
{
}
这意味着 STRUC
在的FUNC
声明 STRUC $之前被使用C $ C>众所周知,C编译器。
解决方案:移动 STRUC
的定义到另一个头文件,并包括此
主要小品:
的#includedatastructures.h无效FUNC(STRUC * P){}无效设置(){
STRUC S;
FUNC(安培; S);
}无效循环()
{
}
和 datastructures.h
:
结构STRUC
{诠释A,B;
};
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 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;
};
这篇关于Arduino的:结构指针作为函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文