Passing structure to function error

Im trying to use a structure to pass data to a fuction but Im getting an ’ has incomplete type ’ error . What am I doing wrong ? What is anonymous and what is incomplete ? I think all of the objects have been declared.

struct DATA {
         int data1;
         char data2;
};

DATA data_struct;

void myFunction(struct data_struct) {
     int a = analogRead(A0);
     int result = data_struct.data1 + a;
     Serial.println(result);
}

 void setup() {
  Serial.begin(9600);
}

void loop() {

data_stuct.data1= random(1,50);
myFunction(data_struct);
}
void myFunction(struct data_struct) {

Think.. What is the TYPE of the struct you're passing here??

Regards,
Ray L.

The class type you just declared is called DATA. You already used that name properly in the declaration of data_struct variable. How come you completely forgot about it in function parameter declaration?

Also, if you are planning to pass this data_struct object to the function as a parameter, why are you declaring it as a global variable? I can't say I know your full intent, but it looks like your data_struct can easily be made a local variable in loop().

Also, passing a potentially heavy object by value when you don't really need an extra copy is an unnecessary waste of stack memory and processor efforts.

OK C++ was 22 years ago and I wanted to be adventurous and try to make my code a bit more "structured" .

Im thinking the data type is DATA so ...

struct DATA {
         float data1;
         char data2;
};

DATA data_struct;

void myFunction(DATA data_struct) {
     int a = analogRead(A0);
     int result = data_struct.data1 * a;
     Serial.println(result);
}
 void setup() {
  Serial.begin(9600);
}

void loop() {

DATA data_stuct.data1= random(1,50);
myFunction(DATA data_struct);
}

But now its complaing about this line :

DATA data_stuct.data1= random(1,50);

with the "struct_ex:19: error: expected initializer before '.' token" error

I know its obvious to you guys but Im missing a fundamental idea here.

void loop() {

  DATA data_stuct;  // Declare a variable named data_stuct with type DATA

  data_stuct.data1= random(1,50);  // Give an element in data_stuct a value

  myFunction(data_struct1);  // Use data_stuct

}
void myFunction(DATA  &  data_struct) {  // Pass by reference to make yourself healthy, wealthy, and wise

Thank you all . That did the trick ! Plus I saw some of my blatent typos !

struct DATA {
         float data1;
         char data2;
};

DATA data_struct;

void myFunction(DATA data_struct) {
     int a = analogRead(A0);
     int result = data_struct.data1 * a;
     Serial.println(result);
}
 void setup() {
  Serial.begin(9600);
}

void loop() {

DATA data_struct;

data_struct.data1= random(1,50);
void myFunction(DATA &data_struct);
}

buckstucky:
That did the trick !

No, that didn't. The code is compilable but nonsensical. And it is not calling your function.

  1. You no longer need the global variable. It is not used in your code anymore.
  2. No need to pass the variable by value
void myFunction(const DATA &data_struct) {
     int a = analogRead(A0);
     int result = data_struct.data1 * a;
     Serial.println(result);
}
  1. What on Earth is this doing at the end of loop()?
void myFunction(DATA &data_struct);

What are you trying to do by this??? To call the function? A function call in C++ looks as follows

myFunction(data_struct);

That's it.

Why do you recommend passing by reference? The function isn't modifying the argument.

Jiggy-Ninja:
Why do you recommend passing by reference? The function isn't modifying the argument.

That simply means that it should be a reference-to-const

void myFunction(const DATA &data_struct)

Jiggy-Ninja:
Why do you recommend passing by reference?

Pass-by-value is 5 bytes. Pass-by-reference is two. (using code from post #3)

But, the quantity is less important than the pressure from three additional registers being reserved for the extra parameter data. In other words, in this case, pass-by-reference gives the optimizer more latitude.

Or are you asking why I chose by-reference instead of by-pointer?

"What on Earth is this doing at the end of loop()?"

Is this wrong ? I was taking the advice of coding-badly and yes I was indicating that it was compiling.

buckstucky:
"What on Earth is this doing at the end of loop()?"

Is this wrong ? I was taking the advice of coding-badly and yes I was indicating that it was compiling.

You misunderstood something.

This is what your code might/should look like

struct DATA {
  float data1;
  char data2;
};

void myFunction(const DATA &data_struct) {
  int a = analogRead(A0);
  int result = data_struct.data1 * a;
  Serial.println(result);
}

void setup() {
  Serial.begin(9600);
}

void loop() {
  DATA data_struct;
  data_struct.data1 = random(1,50);
  myFunction(data_struct);
}

Albeit it is not clear what float type is even doing here and why you decided to involve floating-point arithmetic.


You can also shorten the

  DATA data_struct;
  data_struct.data1 = random(1,50);

to

  DATA data_struct = { (float) random(1,50) };

Or, if you so desire, you can compact your whole loop() into an one-liner

void loop() {
  myFunction({ (float) random(1,50) });
}

(It doesn't mean that it is a good idea to compact it like that. Just to demonstrate some language features.)