Passing struct to function (again)

Hi. (my first post here)

I have defined a struct, but cannot define a function with that type as parameter:

error: 'ALARM' was not declared in this scope In function 'boolean CheckAlarm(ALARM*)':
Bad error line: -2

I see in the Playground and in other (older) posts that the suggestion/solution is to move the struct definition to another file. (or the function too maybe?)

If that's the solution...

But can anybody post me a simple example on that.
I get alot of errors because byte is not defined when the struct is located in the separate file.
Can I just include some file in the .h file or ...?

/Jan

(using 0017)

Create a new tab in the Arduino IDE, name is somefilename.h. Then at the beginning of your program have #include "somefilename.h"

In the .h file you can define all the structs you want.

Is ALARM a class or a struct? If it's a struct, the argument type is "struct ALARM *".

OMG :-[. I better find my old C-bible again.
Years of C# programming...
You were right, PaulS. I forgot "struct" in the argument.
Thanks.

But it was not necessary to locate the struct in a separate h-file.
Is that an obsolete advise?

But it was not necessary to locate the struct in a separate h-file.

No, it wasn't.

I'm new to C structs and am having some problems with this concept. The code examples above aren't complete enough for me to understand.

I'm trying to declare a structure that can be used to hold the results of an operation.

This code works, but it hard codes the structure into the function. I want to be able to pass multiple instances of the structure into the function via a pointer. Ideally, I'd be able to have multiple instances of my structure using something like:

MyStructureType new_struct;

And then pass that newly initialized struct to the function.

working code, but not very flexible:

struct {
  int cmd_ACK;
  String raw_result;
  int cmd_results[8];
} ctlrResults;

void setup () {
  do_something();
  Serial.begin(115200);
  Serial.print("Raw Results:");
  Serial.println(ctlrResults.raw_result);
}

void loop() {
}

void do_something() {
  ctlrResults.cmd_ACK = 1;
  ctlrResults.raw_result = "M1:323:M2:435";
  ctlrResults.cmd_results[0] = 323;
  ctlrResults.cmd_results[1] = 435;
}

The following doesn't work, but is closer conceptually to what I want to do.

struct {
  int cmd_ACK;
  String raw_result;
  int cmd_results[8];
} ctlrResults;

void setup () {
  do_something(&ctlrResults);
  Serial.begin(115200);
  Serial.print("Raw Results:");
  Serial.println(ctlrResults.raw_result);
}

void do_something(struct a* ) {
  a.cmd_ACK = 1;
  a.raw_result = "M1:323:M2:435";
  a.cmd_results[0] = 323;
  a.cmd_results[1] = 435;
}

You can use arrays, references, or pointers to do what you want. The following uses a pointer.

I have a preference for typedefs...

typdef struct {
  int cmd_ACK;
  String raw_result;
  int cmd_results[8];
} ctlrResults_t;

ctlrResults_t ctlrResults;  // This is a global variable

void setup () {
  do_something( &ctlrResults );
  Serial.begin(115200);
  Serial.print("Raw Results:");
  Serial.println(ctlrResults.raw_result);
}

void loop() {
}

void do_something( ctlrResults_t* r ) {
  r->cmd_ACK = 1;
  r->raw_result = "M1:323:M2:435";
  r->cmd_results[0] = 323;
  r->cmd_results[1] = 435;
}

Thanks for the quick response.

The code did not compile for me. I got the following errors:

Tried a few things to fix it but failed... ??

etch_oct05c:-1: error: variable or field 'do_something' declared void
sketch_oct05c:-1: error: 'ctlrResults_t' was not declared in this scope
sketch_oct05c:-1: error: 'r' was not declared in this scope
sketch_oct05c:0: error: expected constructor, destructor, or type conversion before 'struct'
sketch_oct05c:4: error: expected constructor, destructor, or type conversion before ';' token
sketch_oct05c:6: error: 'ctlrResults_t' does not name a type
sketch_oct05c:7: error: variable or field 'do_something' declared void
sketch_oct05c:7: error: 'ctlrResults_t' was not declared in this scope
sketch_oct05c:7: error: 'r' was not declared in this scope
sketch_oct05c.cpp: In function 'void setup()':
sketch_oct05c:10: error: 'ctlrResults' was not declared in this scope
sketch_oct05c:10: error: 'do_something' was not declared in this scope
sketch_oct05c.cpp: At global scope:
sketch_oct05c:19: error: variable or field 'do_something' declared void
sketch_oct05c:19: error: 'ctlrResults_t' was not declared in this scope
sketch_oct05c:19: error: 'r' was not declared in this scope

Sorry about that. Try this...

  • Start the Arduino IDE
  • Load the Sketch
  • On the right side of the IDE's window towards the top is an arrow pointing to the right. Click it. A context menu should appear.
  • Click New Tab
  • Type MyTypes.h and click OK
  • Copy-and-paste the following into the new tab...
#ifndef MyTypes_h
#define MyTypes_h

#include <WString.h>

typedef struct {
  int cmd_ACK;
  String raw_result;
  int cmd_results[8];
} ctlrResults_t;

#endif
  • Navigate to your Sketch using the tabs below the button bar
  • Remove the structure definition from the top of the Sketch
  • Add the following line to the very top of your Sketch...
#include "MyTypes.h"

The Sketch should look like this...

#include "MyTypes.h"

ctlrResults_t ctlrResults;  // This is a global variable

void setup () {
  do_something( &ctlrResults );
  Serial.begin(115200);
  Serial.print("Raw Results:");
  Serial.println(ctlrResults.raw_result);
}

void loop() {
}

void do_something( ctlrResults_t* r ) {
  r->cmd_ACK = 1;
  r->raw_result = "M1:323:M2:435";
  r->cmd_results[0] = 323;
  r->cmd_results[1] = 435;
}

Thanks! Works great. I think I'm starting to wrap my brain around it.

http://www.arduino.cc/playground/Code/Struct#FAQ

:slight_smile: