Passing Value to Function using Struct argument

I found a great way to pass a series of values over serial comms using a Structure (struct) from some code suggested by GroundFungus in this forum.

I'd like to use the struct nomenclature as an argument to my function call:

struct EspStruct {
  byte bMode;
  int iBand;
  boolean boolTX; 
};
EspStruct EspData = {0, 0, 0};


void SendToNano(byte EspData.bMode, int EspData.iBand, boolean EspData.boolTX) {

but this gives me an error message: "expected ',' or '...' before '.' token"

I tried:
byte (EspData.bMode),
byte (EspData).bMode,
but I can't seem to get it to parse.

Am I forced to use:

void SendToNano(byte Mode, int Band, boolean TxStatus) {
  EspData.bMode = Mode;
  EspData.iBand = Band;
  EspData.boolTX = TxStatus;

I would just as soon set the structure values as arguments to the "SendToNano()" function, save some code... I have another similar structure that I'm sending back that has 7 arguments.

My other option is to set and use the structures globally, but I was taught to try to stay away from globals.

Sir Michael

There are a couple of options. Usually by reference is preferred so that the data does not have to be copied, taking up extra space. Use by value if you don't want the possibility of the data being modified.

Sorry, I didn't explain my code well enough for what I'm doing. I'm not trying to pass in a struct in the call to the function. In the call to the function, I have some regular variables that will be loaded in the struct which is passed through comms.

loop() {
  byte Mode = 5;
  int Band = 3;
  boolean TxStatus = 0;

  //Call the function
  sendToNano(Mode, Band, TxStatus);

So the function and the args that I'm trying to do is to put the values directly into the structure:

struct EspStruct {
  byte bMode;
  int iBand;
  boolean boolTX; 
};
EspStruct EspData = {0, 0, 0};


void SendToNano(byte EspData.bMode, int EspData.iBand, boolean EspData.boolTX) {

Sir Michael

Hello SirMichael

Add the function as member function to the struct simply.

1 Like

It looks like I can't put the struct elements as the Function arguments, but I can use the format:

EspStruct{b, i, t};

to load the values in with a single line.

I can make that work!
Thanks everyone!

Sir Michael

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.