How do you pass a struct array member to a function?

I am confused about referencing arrays that are part of a struct.


void printarray(double  *array, int decimalplaces = 0) {

    for (int i = 0; i < 5; i++) { 
        Serial.printf("%.*lf", decimalplaces, array[i]); Serial.print(", "); 
    }
    Serial.print('\n');

}

void setup(){

double test[5] = {1.0,2.0,3.0,4.0,5.0};

struct Sensor{
double a1;
double b1;
double test2[5]={1.0,2.0,3.0,4.0,5.0};
}

Sensor thisSensor;

Using this code, printarray(test); prints 1, 2, 3, 4, 5,

but printarray(thisSensor.test2); prints 0.

I thought maybe thisSensor->.test2 would work, but the compiler complains that Sensor is not a pointer type.

As I said, I'm confused.

re-arrange things a bit..
try this..

struct Sensor {
  double a1;
  double b1;
  double test2[5] = {1.0, 2.0, 3.0, 4.0, 5.0};
};

Sensor thisSensor;

void printarray(double  *array, int decimalplaces = 0) {

  for (int i = 0; i < 5; i++) {
    Serial.printf("%.*lf", decimalplaces, array[i]); Serial.print(", ");
  }
  Serial.print('\n');

}


void setup() {
  Serial.begin(115200);

  double test[5] = {1.0, 2.0, 3.0, 4.0, 5.0};
  printarray(test, 0);
  printarray(thisSensor.test2);
}

void loop() {

}

prints the same now..
have fun.. ~q

@qubits-us what did you actually change or fix?

I had to add a semicolon and a loop90 function to @rp58's snippet. I had to make up for not having printf around. Maybe another syntax detail, forget already, trivial in any event.

Other than that, the OP's expressions were fine, the order of things in the source file were fine, the code worked... fine.

So I gots to know in what way you fixed the code, and what you thought was wrong with

 printarray(thisSensor.test2);

Here's the OP's code with my fixes to its real problems:

void printarray(double  *array, int decimalplaces = 0) {

  for (int i = 0; i < 5; i++) {
    char bufffer[80];
    dtostrf(array[i], 10, 5, bufffer);
    Serial.print(bufffer); Serial.print(", ");
  }
  Serial.print('\n');
}

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

double test[5] = {1.1,2.2,3.3,4.4,5.5};

struct Sensor{
  double a1;
  double b1;
  double test2[5]={10.111,20.222,30.333,40.444,50.555};
};

Sensor thisSensor;

void loop()
{
  printarray(test);
  printarray(thisSensor.test2);

  Serial.flush();

  for (; ;);

}

TIA

a7

maybe you should read what I posted..
Didn't say I FIXED anything, are you just looking to bash me again..
back off me, last warning..

~q

The implication is that you fixed something, so that it prints the same now, whereas it didn't print the same before.

My point was only that there was nothing wrong except incompleteness in the OP's exposition of her problem.

I apologize if you were offended or think I was coming after you again (?), that was not my intent.

TBH I was sure (afraid) I was missing something subtle about what you did that fixed a problem that I did not see as any problem.

So my request for you to elaborate.


Without seeing the actual code that generated this error message, I am wondering if you may have had this:

  Sensor.test2

Please post the code that generated the error you quote. Some of us still learn things every day, I am curious about where you really did go wrong. As you have seen, you had the expression correct in the first place.

    thisSensor.test2

is exactly a pointer to an array of doubles.

a7

What happens if you try this?

void setup(){

double test[5] = {1.0,2.0,3.0,4.0,5.0};

struct Sensor{
double a1;
double b1;
double test2[5]={1.0,2.0,3.0,4.0,5.0};
}

Sensor thisSensor;

memcpy(thisSensor.test2, test, sizeof(test));
printarray(thisSensor.test2);

or

void setup(){

double test[5] = {1.0,2.0,3.0,4.0,5.0};

struct Sensor{
double a1;
double b1;
double test2[5];
}

Sensor thisSensor = {0.0, 0.0, {1.0,2.0,3.0,4.0,5.0}};

printarray(thisSensor.test2);

If you send a pointer to an array to a function, it is common/best practice to send the size with it...

void f(double* arrayPtr, const int arraySize)

Your function will only work for arrays of size 5...

1 Like

The struct declaration has initial values for the double array.

I was somewhat surprised to see that work.

In my version of the OP's code, I made the constants in the arrays different, just so I was sure what was what.

Now googling a bit I find

When we define a struct (or class) type, we can provide a default initialization value for each member as part of the type definition. This process is called non-static member initialization, and the initialization value is called a default member initializer.

Which I def did not learn before this day. So Imma guess it's some kind of modern feature, or I am good at forgetting things.

a7

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