definition of multiple int values

Hi,
I have multiple int:

int val1 = 0;
int val2 = 0;
.
.
.
int val24 = 0;

But coding must be easier than listing all values.

I’m try function “for”:

int val;
int count = 10;

void setup {
for(int i = 0; i < count; i++) {
val = 0;
}
}
Result: error: invalid types ‘int[int]’ for array subscript
Hah, where i’m making a mistake? Thanks

Your mistake is pretending that by putting numbers on the end of your variable names you can treat it as an array. Why don't you take a minute to read a page on arrays in c++ and see how it is really done. What you want is totally doable but you can't just make up your own syntax for it.

int val[10] ;

Or: int val[24]

Thank a lot!

right code:

int val[10];

void setup {
  for(int i = 0; i < 11; i++) {
     val[i] = 0;
}
}

Sometimes it’s best to read manual or use google…

Wrong.

You are writing past the end of the array. Make that

 for(int i = 0; i < 10; i++) {

MarkT:
Wrong.

You are writing past the end of the array. Make that

 for(int i = 0; i < 10; i++) {

That’s not a whole lot better. Using an int to count to 10 is silly.

Correct but not efficient is always better than Wrong.

When I’m use “count” not out of the array, then value “val[6]” from Serial.print is “1026”. (I’m use shorter code to 6)

int val[6];

void setup() {
  Serial.begin(9600);
  for (int i = 0; i <= 6; i++) {
    val[i] = 0;
  }
}
void loop() {
  Serial.println(val[1]);
  delay(1000);
  Serial.println(val[2]);
  delay(1000);
  Serial.println(val[3]);
  delay(1000);
  Serial.println(val[4]);
  delay(1000);
  Serial.println(val[5]);
  delay(1000);
  Serial.println(val[6]);
  delay(1000);
}

MarkT: Correct but not efficient is always better than Wrong.

Well, there is that.

When I'm use "count" not out of the array, then value "val[6]" from Serial.print is "1026".

val[6] is NOT an element of a 6 element array. Quit reading from, or writing to, memory that is NOT part of you array.

Change

  for (int i = 0; i <= 6; i++) {

to

  for (int i = 0; i < 6; i++) {

This is exactly the same mistake expressed differently.

My result is: when I'm need 6 int values, then I heve use it "int val[7]", because last "val[7]" is invalid. Equal was my a misprint.

When you need six int values, then you need "int val[6];"

C/C++ may not be intuitive, but it is certainly not perverse or mendacious.

As Paul tried to point out, arrays in C start with element 0, not 1. Because of that, you don’t have a val[6]. You have val[0] through val[5]. Your code compiled to 2400 bytes. The code below compiles to 2236 and would be a more common way to write your program.

int val[6];

void setup() {
  Serial.begin(9600);
//  for (int i = 0; i < 6; i++) {
//    val[i] = 0;
//  }

  memset(val, 0, sizeof(val) / sizeof(val[0]));

}
void loop() {

  for (int i = 0; i < sizeof(val) / sizeof(val[0]); i++) {
    Serial.println(val[i]);
    delay(1000);
  }

/*
  Serial.println(val[0]);     // C arrays start with 0, not 1
  delay(1000);
  Serial.println(val[1]);
  delay(1000);
  Serial.println(val[2]);
  delay(1000);
  Serial.println(val[3]);
  delay(1000);
  Serial.println(val[4]);
  delay(1000);
  Serial.println(val[5]);
  delay(1000);
  Serial.println(val[6]);     // You don't have one of these!!
  delay(1000);
*/
}

Which can be rewriten:

int val[6];

void setup() {
  for (int &x : val) x = 0;
  for (int &x : val) {
    Serial.println(x);
    delay(1000);
  }
}

void loop() {
}

Jacques

  for (int &x : val) x = 0;It's already zeroed; why waste time and code doing it again?

Just rewrote econjack's:

memset(val, 0, sizeof(val) / sizeof(val[0]));

another way.

I was told to never trust a compiler initialising an array with specific values (0 for an int array in this case).

Maybe I should investigate that C++ garantees that int arrays are zeroed on creation.

Jacques

EDIT: on cppreference.com, I read:

The effects of zero initialization are: If T is a scalar type, the object's initial value is the integral constant zero explicitly converted to T. If T is an non-union class type, all base classes and non-static data members are zero-initialized, and all padding is initialized to zero bits. The constructors, if any, are ignored. If T is a union type, the first non-static named data member is zero-initialized and all padding is initialized to zero bits. If T is array type, each element is zero-initialized If T is reference type, nothing is done. As described in non-local initialization, static and thread-local variables that aren't constant-initialized (since C++14) are zero-initialized before any other initialization takes place. If the definition of a non-class non-local variable has no initializer, then default initialization does nothing, leaving the result of the earlier zero-initialization unmodified.

I will therefore trust that C++ compilers initializes int tables to zero from now on.

int val[6];
becomes
int val[6] = {};

I will therefore trust that C++ compilers initializes int tables to zero from now on.

...and floats, and pointers, and longs. . .

Yes, those too.