Using ++ with Array

Guys & gals

Just playing around with arrays, as I will need to include in a sketch, once finished.

float myfloat[5];
float Temperature = 32.5;
float Humidity = 40;
float Battery = 12.4;
float Light = 0.01;
float Count = 0;

void setup() {
  Serial.begin (9600);
  myfloat[0] = Temperature;
  myfloat[1] = Humidity;
  myfloat[2] = Battery;
  myfloat[3] = Light;
  myfloat[4] = Count;
}
void loop() {
  for (int i = 0; i < 5; i++)
  {
    Serial.println(myfloat[i]);
    //            myfloat[i]++;
  }
  Serial.println("");
  Serial.println("");
  Count = Count + 1;
  //Count++;
  //myfloat[4] = 1;
  delay(2000);
}

I will need to update the values of the 5 variables, so I checked to see ensure that I was doining things correctly. So I just put Count++ at the end of loop(). This didn't increament Count nor did Count = Count+1.

The only way seemed to be to use either of the array commands, myfloat*++ or myfloat[4] = 1.*
Does this mean that when I write to the elements of an array, I have to do it with an array command?
I thought, that by updating the variable, then the array would just extract the updated value.

I have to do it with an array command?

I've never heard of an "array command", so I don't know what you mean by this.

You couldn't normally use "++" on a float variable, and it's unclear to me what you hope to do using     myfloat[i]++; , which would add one to each of your temperature, humidity, battery level etc values.

Tell us what you really want to do

Arrays are just a bunch of variables in a row. It's handy to be able to refer to them by number, but apart from that they're just like any other variable.

So when you do

myfloat[4] = Count;

the value in Count is copied into myfloat[4].

If later in the script you change the value of Count, that doesn't have any effect on myfloat[4]. (or vice-versa).

The = operator doesn't form any kind of lasting link between them. It just copies the number from one to the other, then they go their own ways and forget they ever knew each other.

@Groove
The incrementing shown here was just to see if the values updated. They are only for testing, nothing more.

@GypsumFantastic
If I read what you say correctly, then I have to have, effectively, 2 seperate variables for 1 value, eg myfloat[4] and count. As the sketch changed the value of count, I would also have to update myfloat[4].
I thought the idea was that you could change the values in any of the variables, and when you called the array, the array would extract the latest value.
I must be missing something somewhere.

You cannot "call an array".
The values in an array don't magically mirror the variables you assigned them from, nor vice versa.

Groove:
The values in an array don't magically mirror the variables you assigned them from, nor vice versa.

Unless it's an array of pointers...

Regards,
Ray L.

RayLivingston:
Unless it's an array of pointers...

Regards,
Ray L.

I'll repeat, for the hard of understanding, The values in an array don't magically mirror the variables you assigned them from

You can bind variables to array elements using a reference type.

int iArr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

int& elm4 = iArr[4];

void setup() {
  Serial.begin(250000);
  piArr();
  elm4 = 99;
  piArr();
}
void piArr() {
  for (byte i = 0; i < sizeof(iArr) / sizeof(iArr[0]); i++) {
    Serial.write(' ');
    if (iArr[i] < 10) {
      Serial.write(' ');
    }
    Serial.print(iArr[i]);
  }
  Serial.println();
}
void loop() {}
  1  2  3  4  5  6  7  8  9 10
  1  2  3  4 99  6  7  8  9 10

...but you can't have an array of references.

Groove:
...but you can't have an array of references.

Who wanted that?

IamFof:
I thought the idea was that you could change the values in any of the variables, and when you called the array, the array would extract the latest value.

References to array elements behave like that

IamFof:
If I read what you say correctly, then I have to have, effectively, 2 seperate variables for 1 value, eg myfloat[4] and count. As the sketch changed the value of count, I would also have to update myfloat[4].

Yep! You have 2 entirely separate variables. You made them have the same value temporarily, but that's about it.

Arrays are just variables with an index. The index means you can access them in convenient ways. That's just about the whole story I'm afraid.

I thought the idea was that you could change the values in any of the variables, and when you called the array, the array would extract the latest value.
I must be missing something somewhere.

There's a thing called pointers that can do something like that, but it's not what arrays are.

Don't worry about pointers for now though. Get a C/C++ book (or do some online tutorials) and get the hang of arrays first.

Sorry mate! :slight_smile:

@Whandall

   "I thought the idea was that you could change the values in any of the variables, and when you called the array,  
the array would extract the latest value."

References to array elements behave like that

You appear to be indicating that if I change Count from 0 to 54, say, in the program, and then when I use Serial.print to print the contents of myfloat[4], it would print 54. Am I reading you correctly?

If so, that contradicts what @Groove was indicating:

If later in the script you change the value of Count, that doesn't have any effect on myfloat[4]. (or vice-versa).

The = operator doesn't form any kind of lasting link between them. It just copies the number from one to the other, then they go their own ways and forget they ever knew each other.

If "count" is a reference to "myFloat[4]", then yes, changes in "count" will be reflected in "myFloat[4]".

float& count = myFloat [4];

Couple of ways to do this.

float Temperature = 32.5;
float Humidity = 40;
float Battery = 12.4;
float Light = 0.01;
float Count = 0;

float *myfloat[5] = {
  &Temperature,
  &Humidity,
  &Battery,
  &Light,
  &Count
};
float myfloat[5] = {32.5, 40,12.4,0.01, 0};
float& Temperature = myfloat[0];
float& Humidity = myfloat[1];
float& Battery = myfloat[2];
float& Light  = myfloat[3];
float& Count = myfloat[4];
float myfloat[5] = {32.5, 40,12.4,0.01, 0};
#define Temperature myfloat[0]
#define Humidity myfloat[1]
#define Battery myfloat[2]
#define Light   myfloat[3]
#define Count myfloat[4]
struct SomeFloats {
  float Temperature = 32.5;
  float Humidity = 40;
  float Battery = 12.4;
  float Light = 0.01;
  float Count = 0;
} thefloats;

#define myfloat ((float *)&thefloats)

And probably the best way:

union {
  struct {
    float Temperature = 32.5;
    float Humidity = 40;
    float Battery = 12.4;
    float Light = 0.01;
    float Count = 0;
  } asNames;
  float asArray[5]
} theFloats;

But really that answer is: you don't normally want to do this. These are not only separate values, they are separate kinds of value. They each have their own rules, their own expected range, and their own way of being formatted for output - some should be printed with a percentage, some might be positive or negative wheras others will always be positive and nonzero. Some are always integers, others might be a real floating point. They will each occupy their own slot on the LCD, if you are using one.

Just print the five values.

@Groove

In my original sketch

myfloat[4] = Count;

I presumed therefore, that if I update the data in Count, and then at some point Serial.print(myfloat[4]), I would get the updated value in Count.

This is, unless I am way of the mark, what you indicated in your last post, and what Whandall said

 "I thought the idea was that you could change the values in any of the variables, and when you called the array,  
the array would extract the latest value."

References to array elements behave like that

Yet in my sketch, this just doesn't happen. What am I missing?

@PaulMurrayCbr

Thanks for the different options. I will look into these.

In the final sketch there will be something in the order of 10 variables, most of which will be float variables, with a couple of ints, but these will just have to be floats to go into the array.

I know I could just print out the values, but decided that this would be a good opportunity to get my head around arrays. Groan!! :smiling_imp:

What am I missing?

"count" must be a reference, not a variable.

"I presumed therefore, that if I update the data in Count, and then at some point Serial.print(myfloat[4]), I would get the updated value in Count. "

Kinda depends on the sequence in which things happen.

Count = 11;
myfloat[4] = 11;

Serial.println(myfloat[4]); // should print '11'.

However,

myfloat[4] = Count;
Count =12;
Serial.println(myfloat[4]); // will print whatever myfloat[4] gets from the first line, as Count will not flow 'backwards' into the array.

IamFof:
This is, unless I am way of the mark, what you indicated in your last post, and what Whandall said

Yet in my sketch, this just doesn't happen. What am I missing?

The part that said that references worked like that.

Take a look at the snippet posted in #7. There is an ampersand (&) in the declaration of elm4. Your code doesn't have that. Try learning what that & means.

It's not as nice as using suitably named variables but you can use ++ with the array element if you want.

anArray[someElement]++;

You could also use named variables as the index value to make the code more readable if you were determined to use an array

  byte count = 2;
  anArray[count] = 123;
  anArray[count]++;

IamFof:
@Groove

In my original sketch

myfloat[4] = Count;

I presumed therefore, that if I update the data in Count, and then at some point Serial.print(myfloat[4]), I would get the updated value in Count.

This is, unless I am way of the mark, what you indicated in your last post, and what Whandall said

 "I thought the idea was that you could change the values in any of the variables, and when you called the array,  

the array would extract the latest value."

References to array elements behave like that




Yet in my sketch, this just doesn't happen. What am I missing?

What you're missing is that it DOES NOT work like that. When you do:

myfloat[4] = Count;

The current value of Count is read. That value is then written to myfloat[4]. Period. End of story. It makes no difference what you do to Count after that, myfloaat[4] DOES NOT CHANGE unless and until you again explicitly write it.

If you take a photograph, and print it out, then take another photograph of some other subject, do you expect the photograph you printed to change to reflect the new subject? Of course not. This works exactly the same way.

Regards,
Ray L.