How do get the array length?

When I run that in the Arduino to verify it, I get no errors.

So when you write code and the code compiles that means there are is nothing wrong with the code logic and there are no bugs in your code.
WOW! I did not realize that.
NOT!

int x [] = {}; // empty array
char buffer [20];
void setup ()
{
  Serial.begin (115200);
  sprintf (buffer, "%p", x);
  Serial.print ("x@ ");
  Serial.println (buffer);

  sprintf (buffer, "%p", buffer);
  Serial.print ("buffer@ ");
  Serial.println (buffer);
}

void loop ()
{}

Enjoy.

o you’d’ve remembered all that, wouldn’t you?

Even if you didn’t, and you were using dynamic allocation, the size of the array, at any point in time, is fixed. Divide the number of bytes in the array by the number of bytes in one element to get the number of elements - exactly as you’ve been advised since reply #1.

What I find absolutely astounding it the attitude here.

I am trying to learn what this device can do and come up with some of my own demonstrations and during the journey make a few discoveries as well as learn its limitations.

What does not help is attitudes of people, especially moderators and with regard to programming experience, counts for very little when you can not be bothered to sit and explain why you think that something can not be done.

During my experience of programming, not just JavaScript but a smattering of C and C type languages and OO, often the teacher is limited by what they were taught that can and can not be done.

I admit that its been a while since using a compiled language but that does not mean that you can take the piss, so if you can't be assed to help, don't bother answering, simple really.

What I find absolutely astounding is the attitude here.

often the teacher is limited by what they were taught that can and can not be done.

Sounds like a pretty good attribute for a teacher. I mean, you wouldn't want to teach what can't be done, would you?

(I teach SCUBA diving. My store-bought gear will take me down, on air, to probably about 200 metres or a lot more. It's really ordinary quality gear. That would be really, really stupid, so I teach people not to do it. If people want to swim down past me, I will remonstrate with all the vehemence non-verbal communication allows, but I'm not going to go down after them much beyond about 45 metres)

programming experience, counts for very little when you can not be bothered to sit and explain why you think that something can not be done

It's not that I "think" it can't be done, it's way deeper than that.

What does not help is attitudes of people, especially moderators

We're human too. Get over it.

I am trying to learn what this device can do and come up with some of my own demonstrations and during the journey make a few discoveries as well as learn its limitations.

We had a similar evangelist a few days ago who exhorted us to try hooking up LEDs and trying stuff like analogWrite (LEDpin, 1023) and see how bright it was. But was analogWrite (LEDpin, 1024) on a UNO any brighter? No, of course it wasn't.

Use the standard C idiom for arrays…

#define MYARRAY_SIZE 10

int myArray[MYARRAY_SIZE];

for (i=0;i<MYARRAY_SIZE;i++) {
  myArray[i] = 0;
}

If you need to pass the array to a function, pass the length also.

MarkG:
You have an array…

int x[] = {}; // empty array

Exactly. It can hold ZERO elements.

you make a random number between 50 and 200

long y = random(50, 200);

OK. Useless, but, hey, it’s your story.

you run a loop to then fill that array with a random number where the loop is 50 to 200 numbers

for(int c=0; c<=y-50; c++){

x[c] = random(1, 10);// stick a random number in it 1 to 10
}



Doing that is stupid, because there is space in the array for ZERO elements. You've, therefore, written over space that you don't own. That often has disastrous results.

When I run that in the Arduino to verify it, I get no errors.

Happy?

Yeah, but you won't be if you keep this crap up.

One last try:

The reason you’re getting what you perceive to be attitude is your insistence on continuing to assert something that every respondent in the thread knows to be false.

It’s not people being limited by what they were taught, it’s a bunch of folks who know very well how the C language works - IIRC, I was coding in it professionally in 1985. Arrays in C are very rudimentary and they most certainly don’t dynamically expand if you access elements outside their bounds. Come to that, they don’t do bounds checking either, so you can write stuff such as your code example that compiles but will be a disaster when it runs. The compiler doesn’t care that you wrote on memory outside the array - it trusts you to know what you’re doing. Take a look at a C tutorial - you’ll find no mention of the mechanism you suggest exists - you’ve simply confused it with another language.

Try declaring some character arrays with “Hello world” or the like either side of your zero length array. Print them at the start of your sketch, run your array filling code above, print them again. If you reduce the range of your random numbers a bit, you will find that what you thought would expand the zero length array actually stamped all over one of your “Hello world” messages.

C is powerful, but it hands you a loaded gun and expects you to know how to use it. Bad things will happen if you don’t.

The biggest calibre loaded gun of all so far

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:

Elements were added to the array alright,

No, memory you didn't own was dumped on. All wrong.

Why is this? Not really random, are they.

No, they are pseudo-random and because you didn't seed the the pseudo-random number generator with a random number, it returned the same sequence.

MarkG: Yes, but that still doesn't get around the fact that you can have a zero length array.

No, zero length arrays are not legal. It is a hack, and is only intended for use within a struct as a header for a variable length object. It is provided by GNU, C1 and a few others, but is not legal C or C++. Flexible arrays are different, but only available in C.

If you open your platform.txt and add -pedantic to the command line, you'll receive this error:

overwrite.ino:1: error: ISO C++ forbids zero-size array 'array'

@boguz, To see the full extent of what your code was doing, this simple sketch will show you. the array is only read from, the variable number is incremented. Reading the array out of bounds has read 'number'.

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

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

void loop() {
  ++number;
  Serial.println( array[ 2 ] );
  delay( 500 );
}

@boguz:

But it is kind of useless if i already know the size of the array anyway.

It’s real purpose is to make changing things easier in the code, and to automatically adjust the count if you cross platform. For example, You often see things like:

int myArray[10];
// ...a bunch of code...

for (i = 0; i < 10; i++) {
   // for loop code...

Now if you decide to change the size of the array, you must go through the entire source file and change every instance of the constant used in the second expression of the for loop. Using the macro automatically adjusts the loop if you use:

int myArray[10];
// ...a bunch of code...

for (i = 0; i < HowBigIsThisArray(myArray); i++) {
   // for loop code...

Now, if you change the size of the array, it’s automatically adjusted throughout the program.

The other advantage is that the macro can be used with any data type, so HowBigIsThisArray() can be passed a float, long, int, or byte array and it returns the correct length. If you move the code to a platform where an int is 4 bytes rather than 2, it still returns the correct number.

In short, the marco reduces the number of things you might otherwise have to change and, as a general rule, the fewer edits you make to working code, the better.