Set but not used? Really?

Without Serial.println(B1); I get a warning. With it no warning. I have this array of bools that I reset to false every day. Seems to be working, but I don't like errors or warnings.

static bool InternetIntervention[] = {false, false, false, false, false, false, false};

for (auto B1 : InternetIntervention) {
          //Serial.println(B1);
          B1 = false;
          //Serial.println(B1);
        }

Users/billbe/BackupArduino/WIFIRev2ExteriorJun262021PROD/WIFIRev2ExteriorJun262021PROD.ino:169:19: warning: variable 'B1' set but not used [-Wunused-but-set-variable]
for (auto B1 : InternetIntervention) {

should be

InternetIntervention[B1] = false;

Ok, but seems like using this range array thing I was the thing before the colon. I will try it that way tho. Thanks.

You have made B1 the loop index. If it confuses you, use the traditional for loop syntax.

Ok, but this one works, is it wrong too.

for (const char *Switch : SwName) {
strcat(html, Switch);

Please post complete example sketches, not snippets. Also, when you say something is "wrong" we have no idea in what way it is wrong. Please always specify that.

It's not, see Range-based for loop (since C++11) - cppreference.com.

B1 is a copy of each element in the array, setting the copy to false doesn't change the array at all. If you want to use it to modify the elements in the array, use a reference to the elements:

for (auto &B1 : InternetIntervention) {
  B1 = false;
}
1 Like

oh, ok. what would 'InternetIntervention[B1] = false; ' do then?

odd that for a bool use &reference and for char use *pointer

oops. sorry.

static bool InternetIntervention = {false, false, false, false, false, false, false};

for (auto B1 : InternetIntervention) {
Serial.println(B1);
B1 = false;
Serial.println(B1);
}

You can use a reference too. The thing is the copy of the pointer will still point to the same place, so if you mess around with the data that is pointed at, the original would be modified. Not the case with your bool arrray, you just modify the temporary copy (exists only within the scope of the for range loop, within the {})

What does the double-& syntax on that page do?

It is safe, and in fact, preferable in generic code, to use deduction to forwarding reference:
for (auto&& var : sequence)

Ensures job security for the guy that uses it as other programmers feel ill upon gazing at it.

it's for advanced use (or default use if you don't know)... if the initializer is an lvalue, auto&& and auto& will lead to the same behavior, but If the initializer is an rvalue then auto&& works (and yields an rvalue reference) whereas auto& will fail to compile.

see Value categories - cppreference.com for more details

Is it fair to say that a pointer is an area that holds an address and a reference is an address computed only at compile time?

pointers can be indexed, references cannot.
pointers are mutable, references are not.
pointers can be NULL, references cannot.
pointers have to be explicitly dereferenced (with *), references are implicitly dereferenced

1 Like

The easiest way to see that is that indeed a pointer is a variable that holds the memory address (usually of another object) whereas a reference is an alias (another name) for an already existing variable or you can see that as a constant pointer with automatic indirection.

So indeed one consequence is that a pointer can be null (point nowhere) whereas a reference must be initialized (bound to an existing object) when it is declared.

1 Like

which begs the question of why do I need n alias?

it's often used when you call a function and want to modify the original value

void changeValue(int & x) { x = 3;}

if you have two int variable a and b and call changeValue(a); and changeValue(b); then you'll end up with 3 in a and b