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 answer above works. In the meantime I had found the following also to work, without the need for a .h file:
Be warned: Arduino coding is a little flaky. Many of the libraries are also a bit flaky!
This next code works for me as in Arduino 1.6.3:
The problem is, that the Arduino-IDE auto-translates this into C like this:
Which means
Struc
is used in the declaration offunc
beforeStruc
is known to the C compiler.Solution: Move the definition of
Struc
into another header file and include this.Main sketch:
and
datastructures.h
:Prolly old news, but
typedef struct
allows member functions (at least in IDE 1.6.4). Depending on what you want to do, of course, but I can't think of anyfunc(struct *p)
that couldn't be handled also withobject.func(param pdata)
. Just that something likep->a = 120;
becomes something likeobject.setA(120);