How do get the array length?

i was curious about this sizeof function (i have never used it before), so i decided to write a little sketch to try it out and see how it works.
i wrote something like this:

int array1[6] = {1,2,3,4,5,6};
byte array2[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
boolean array3[] = {1,1,0,0};

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

void loop() {
  byte array1size = sizeof(array1) / sizeof(int);
  byte array2size = sizeof(array2) / sizeof(byte);
  byte array3size = sizeof(array3) / sizeof(boolean);

  Serial.print(array1size);
  Serial.print('\t');
  Serial.print(array2size);
  Serial.print('\t');
  Serial.println(array3size);

  delay(3000);
}

and i got something like this on the serial monitor:

6     16     4

so i thought, well, this sizeof() actually works well to find out the size of the arrays. But it is kind of useless if i already know the size of the array anyway. :roll_eyes:
Hmmm, maybe if i could add elements to an array...
So i decided to try to write another sketch to test this idea.

(ah, how good is it to know nothing about this things and still try them out. It's like being a child again, trying to figure stuff out and just having fun with it... 8) )

int array[2] = {};
int number;
int count = 0;

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

void loop() {
  number = random(0,255);
  array[count] = number;
  count++;

  for(int i = 0; i < count; i++) {
    Serial.print(array[i]);
    Serial.print('\t');
  }
  Serial.println();

  int arraySize = sizeof(array) / sizeof(int);
  Serial.print("Count: ");
  Serial.println(count-1);
  Serial.print("Array Size: ");
  Serial.println(arraySize);
  Serial.println();

  delay(3000);
}

well, i sure didn't get what i was expecting!!! :astonished:

232	
Count: 0
Array Size: 2

232     19	
Count: 1
Array Size: 2

232     19     158	
Count: 2
Array Size: 2

232     19     158     38	
Count: 3
Array Size: 2

Elements were added to the array alright, but the size was always 2! This was for me surprising, but showed me that we can't change the length of the array.

why is it that in my code above it added values to FOUR elements of the array, even if it shouldn't have so many elements in it?
i run the sketch several times and the "random" ints were ALWAYS the same. Why is this? Not really random, are they...?

also, when i open the serial monitor it prints the results i have shown above but then, instead of count = 4, it "resets" the count and starts over from count = 0 and again it gives array[0] the value of 232,... why?
ai, ai, ai... the more we try to learn, the more we see we don't know anything about it!!!
:stuck_out_tongue:

So, what i mean is, instead of arguing with the other people here in the forum, maybe you can also do some trying and searching and see if you can find out something on your own.
I have also often thought that the comments here on the forum aren't as "patient" and "kind" as they could be, but i guess also not every question (and people asking the question) is as informed and pertinent as they should. People sometimes want to "sound cool" and instead of saying "oh, cool, thanks, i didn't know that", they just keep at it.

Maybe if you would tell us more about your project, what components you are using, what you are trying to achieve,..., and post your code, maybe some one could help you find a good solution...
:wink:

Hmmm, i guess it is time for me to go to sleep!
:sleeping: