Arduino IDE: size(array) ends in error

Hi folks,

I have a simple question. How can I read out the length of an array for a "for loop"?
I would be very happyto get insight to it.

This works with C++ on VS2022 (console application)

#include <iostream>
std::string topics[5] = { "hellopl", "Testpl" };
int main()
{
    for (int i = 0; i < size(topics); i++) {
        std::cout << topics[i];
    }
}

While this on Arduino IDE throws an error:

#include <iostream>
String topics[2]           = {"Hi", "test"};

void loop() {
for (int i = 0; i < size(topics); i++) {
    Serial.println(topics[i]);
  }
}
error: 'size' was not declared in this scope
  109 |   for (int i = 0; i < size(topics); i++) {
      |                       ^~~~

Welcome to the forum

Do you want to know the size of the array in bytes or the number of elements in the array ?

String topics[2] = { "Hi", "test" };

void setup()
{
    Serial.begin(115200);
    Serial.print("bytes used by the array = ");
    Serial.println(sizeof(topics));
    Serial.print("number of elements in the array = ");
    Serial.println(sizeof(topics) / sizeof(topics[0]));
}

void loop()
{
}

1 Like

try

for (int i = 0; i < sizeof(topics)/sizeof(String); i++)

Hi,

Thanks for the quick answers!

The "possible" elements of an array. In my sample its "2" -> just for the loop of priting all defined topics in the array :slight_smile:

I assume that you mean the actual number of elements in the array as it will not change

you need the array identifier (a pointer to the start of the array in menory) and the type of elements, e.g.

  float data[] = { 1.5, 3.142, 9.6, 2.7, 1034.0 };
  for(int i = 0; i < sizeof(data) / sizeof(float); i++)
    Serial.println(data[i]);

prints

1.50
3.14
9.60
2.70
1034.00
2 Likes

Hmm. When I think about it, it might be the best to return the actual entries of the array instead of the possible, yes thats correct and agree with you

Not sure how that's possible since the function size() is indeed undeclared in your code.

Did you mean std::size()?
Or, you could use std:array which has a size() member function.

Both of these work on an ESP32 with the Arduino core:

#include <string>

std::string topics[5] = { "hellopl", "Testpl" };

void setup() {
  Serial.begin(115200);
  delay(1000);
  
  for (int i = 0; i < std::size(topics); i++) {
    Serial.println(topics[i].c_str());
  }
}

void loop() {}
#include <string>
#include <array>

std::array<std::string, 5> topics = { "hellopl", "Testpl" };
void setup() {
  Serial.begin(115200);
  delay(1000);
  
  for (int i = 0; i < topics.size(); i++) {
    Serial.println(topics[i].c_str());
  }
}

void loop() {}

You can also use a for-each loop, which iterates through each element of an array.

String topics[2] = {"Hi", "test"};

void setup() {
  Serial.begin(115200);
  for (auto &i : topics) {
    Serial.println(i);
  }
}

void loop() {
}
1 Like

Note that both "size" functions above return the container's size (5) not the number of assigned elements (2). Will your container have a variable number of elements? If so, std::vector might be a better choice.

I used @horace solution, because this was indeed the solution I've looking for.
empty array's printing is not that usefull after all ^^

I would favour using

sizeof(topics) / sizeof(topics[0]

It does the same thing but will work with data of any type