copying a stgructure

I have this code:

typedef struct { int Pitch; int FMode; } Payload;

Payload TheData, Xdata;

void GetData() { TheData.Pitch = analogRead(9); TheData.FMode = digitalRead(10); }

Xdata = TheData;

I want to copy TheData to Xdata. I get an error stating

'Xdata' does not name a type

I would think this should work, yes/no?

This compiles without errors

typedef struct {
  int Pitch;
  int FMode;
} Payload;

Payload TheData, Xdata;

void GetData()
{
  TheData.Pitch = analogRead(9);
  TheData.FMode = digitalRead(10);
}

void setup()
{
  Xdata = TheData;

}

void loop()
{
  // put your main code here, to run repeatedly:
}

So you have to show more code for us to point out where you went wrong.

If it's all correct, I can't say without testing; but I have my doubts.

To copy, you can use memcpy().

typedef struct {
  int Pitch;
  int FMode;
} Payload;

Payload TheData, Xdata;

void GetData()
{
  TheData.Pitch = analogRead(9);
  TheData.FMode = digitalRead(10);
}

void setup()
{
  memcpy(&Xdata, &TheData, sizeof(Payload));
}

void loop()
{
  // put your main code here, to run repeatedly:
}

Because the code is not properly posted, I cannot tell if it is complete. All executable code must be in a function, and a function should not contain a function.

(Yes, there are exceptions, but they don't apply here.)

I would expect to see functions setup, loop, or main.

sterretje: To copy, you can use memcpy().

Since this is C++, for structs/classes a copy constructor is implicitly defined by default. this means that (since this is a simple struct of POD's) you can make the assignment just like any plain old data types:

typedef struct {
  int Pitch;
  int FMode;
} Payload;

Payload TheData, Xdata;

void setup() {
  Serial.begin(9600);
  TheData.Pitch = analogRead(9);
  TheData.FMode = digitalRead(10);
  Xdata = TheData;
  Serial.println(F("TheData:"));
  Serial.println(TheData.Pitch);
  Serial.println(TheData.FMode);

  Serial.println(F("Xdata:"));
  Serial.println(Xdata.Pitch);
  Serial.println(Xdata.FMode);
}

void loop() {

}

BulldogLowell: Since this is C++, for structs/classes a copy constructor is implicitly defined by default. this means that (since this is a simple struct of POD's) you can make the assignment just like any plain old data types:

Never knew that. I'm still too much of a C programmer and thought it would work the same as C for structs.

I am embarrassed. This is the case where I looked at the simple error over and over and did not see it.

I put the statement Xdata = GetData; outside of the setup procedure. No wonder it didn't work. I will do better.

I didn't add more surrounding code to make the example simple. Like Einstein said, "make things simple but not too simple" or something like that.

You made it too simple :)

barryjo: I put the statement Xdata = GetData; outside of the setup procedure. No wonder it didn't work.

if you want Xdata to always equal TheData:

typedef struct {
  int Pitch;
  int FMode;
} Payload;

Payload TheData;
Payload& Xdata = TheData;


void setup() {
  Serial.begin(9600);
  TheData.Pitch = analogRead(9);
  TheData.FMode = digitalRead(10);
//  Xdata = TheData;
  Serial.println(F("TheData:"));
  Serial.println(TheData.Pitch);
  Serial.println(TheData.FMode);

  Serial.println(F("Xdata:"));
  Serial.println(Xdata.Pitch);
  Serial.println(Xdata.FMode);
}