passing pointer variable

Hello everyone,

I am trying to pass the same variable to numerous methods

main.cpp

#include <Arduino.h>

struct Bridge {

  uint8_t x;
  bool b;

  void flush() {
    x=0; b=false;
  }
};

void myFunction(Bridge* bridge) {

  Bridge b = {10,true};

  bridge -> x = b.x;
  bridge -> b =  b.b;


}

void myOtherFunction(Bridge* bridge) {

  Bridge b = {66,true};
  

  bridge[0] = b;
}

Bridge BRIDGE = {5,false};

void setup() {

  Serial.begin(9600);

  myFunction(&BRIDGE);
  Serial.print(BRIDGE.x); Serial.print(BRIDGE.b);

  BRIDGE.flush();
  Serial.println("------");

  myOtherFunction(&BRIDGE);
  Serial.print(BRIDGE.x); Serial.print(BRIDGE.b);


  delay(500);
}

void loop() {
    // Serial.println("Loop");


    // delay(1000);

}

My question: Is there an efficient way to copy the variable to the passing pointer?

In ‘myFunction’ each value is copied individually (worst solution)
In ‘myOtherFunction’ the passing value is set via the first element of the pointer (this seems kind of dirty).

The variable ‘b’ is created somehow in the method and should be passed to the pointer ‘bridge’.

Thanks for the help

Try:

memcpy(bridge, &b, sizeof(Bridge));

But, that is probably what the default assignment operator does in your 'myOtherFunction()' anyway. If you're going to keep it that way, just do:

*bridge = b;

BTW, kind of poor names choices: Bridge, bridge, BRIDGE. A little confusing.

I will use your solution

*bridge = b;

Thanks.

P.S.: Naming variables is one of the hardest things in programming