Arduino:结构指针作为函数参数 [英] Arduino: struct pointer as function parameter

查看:59
本文介绍了Arduino:结构指针作为函数参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码给出了错误:

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?

代码:

typedef struct
{ int a,b;
} Struc;


void func(Struc *p) {  }

void setup() {
  Struc s;
  func(&s);
}

void loop()
{
}

推荐答案

问题是,Arduino-IDE 会像这样自动将其翻译成 C:

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()
{
}

这意味着在 Struc 被 C 编译器知道之前,在 func 的声明中使用了 Struc.

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

解决方案:将Struc的定义移动到另一个头文件中并包含这个.

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

主要草图:

#include "datastructures.h"

void func(Struc *p) {  }

void setup() {
  Struc s;
  func(&s);
}

void loop()
{
}

datastructures.h:

struct Struc
{ int a,b;
};

这篇关于Arduino:结构指针作为函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆