Array of structures

Нow to calculate the number of elements in the array of structures?

Structure:

struct Str_item
   {
 String text;
 byte X;
 byte Y;
 byte changeable;
 void *PtrNextLevel;
 void *PtrPrevLevel;
    };


Str_item Array2[]=
{
 { "Text5   ", 2, 0, 1, NULL, NULL },                //item1
 { "  Text6", 2, 1, 1, NULL, NULL},                  //item2
 { "   Text7  Text7", 3, 2, 2, 1, NULL, NULL},       //item3
 { "  Text8 ", 2, 3, 1, NULL, NULL}                  //item4
};

In this exаmple there are an array and 4 elemens. How to calculate the number of elements (4) in the array runtime?

Same way you calculate the number of elements in any array - sizeof (array) / sizeof (array[0])

AWOL:
Same way you calculate the number of elements in any array - sizeof (array) / sizeof (array[0])

Thank you, it worked

careful your item 3 does not match the struct

here is something to test the different sizes. As AWOL said - standard approach works.

struct Str_item
{
  String text;
  byte X;
  byte Y;
  byte changeable;
  void *PtrNextLevel;
  void *PtrPrevLevel;
};


Str_item Array2[] = {
  { "Text5   ", 2, 0, 1, NULL, NULL },                //item1
  { "  Text6", 2, 1, 1, NULL, NULL},                  //item2
  { "   Text7  Text7", 3, 2, 1, NULL, NULL},       //item3
  { "  Text8 ", 2, 3, 1, NULL, NULL}                  //item4
};



void setup() {
  Serial.begin(115200);
  Serial.println(sizeof (Array2));
  Serial.println(sizeof (Array2[0]));
  Serial.println(sizeof (Array2) / sizeof (Array2[0]));
  Serial.println(Array2[0].text);
  Serial.println(Array2[2].text);
  
}

void loop() {}

You will note that the String characters are not counted in the structure itself, just a pointer to the object in memory that will be dynamically allocated when the string is instantiated - but the characters are there correctly as demonstrated by the last 2 prints

this is the output

52
13
4
Text5   
   Text7  Text7

You will note that the String characters are not counted in the structure itself, just a pointer to the object in memory that will be dynamically allocated when the string is instantiated

. . . plus the String length and the String capacity, making even an empty String consume six bytes of RAM.

I understood. Thanks to all .

AWOL:
. . . plus the String length and the String capacity, making even an empty String consume six bytes of RAM.

indeed sizeof (String)

AWOL:
. . . plus the String length and the String capacity, making even an empty String consume six bytes of RAM.

I don't know if I've ever used the String class, so I did not know this...thanks! Does this mean that each element in a String array has something like a descriptor block associated with it?

The String class contains three protected data members:

  1. the pointer to the current char array, used to hold the string data - char*
  2. the size of the allocated memory pointed at by 1) - int
  3. the length of the string held in 1) - int

It's all in WString.h and WString.cpp

econjack:
I don't know if I've ever used the String class, so I did not know this...thanks! Does this mean that each element in a String array has something like a descriptor block associated with it?

indeed

https://github.com/arduino/Arduino/tree/master/hardware/arduino/avr/cores/arduino

protected:
	char *buffer;	        // the actual char array
	unsigned int capacity;  // the array length minus one (for the '\0')
	unsigned int len;       // the String length (not counting the '\0')

J-M-L: Thanks for the link! I did not know where the source was for the core. It's really great to see it in one place! +1 !

it's also on your computer when you install Arduino IDE :slight_smile:

On my Mac it's in

/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino

J-M-L:
it's also on your computer when you install Arduino IDE :slight_smile:

True, I just never looked for it beyond looking for main() a long time ago.