Array of strings

In the context of arrays of strings, I've read that using strings is RAM intensive.
So is this ...

const int apple = 1;
const int box = 2;
const int carnage = 3;
const int delicious = 4;
int something[4] = {apple, box, carnage, delicious};

... a valid alternative for this ...

char something[4] = {apple, banana, carnage, delicious};

... and is it any less RAM intensive?

My thinking is that, when I'm reading code, it's a lot easier for me to read apple, box etc and know what they mean, but if there's no inherrant relationship between them, it seems that it shouldn't matter if they're stored as values that do.

(I realise there have been similar posts about this, but I've not seen one that I can see quite addresses the question. And I don't have a specific use yet.)
(And sorry if it's a stupid question.)

You may like to use an enum, a way to bind an identifiers to integers:

enum Items {apple = 1, box, carnage, delicious};

and in your code, apple will work just like 1, box as 2 and so forth.

You may want to start thinking of zero as a perfectly good number; array indices run from 0 to N - 1 if N is the total number of elements in an array.

enum Items {apple, box, carnage, delicious};

where not saying where to start, the default is 0.

Most real code uses 0 .. N - 1, you may find some arguments about that.

And yes, less RAM intensive.

a7

1 Like

You don't have an array of strings. You have an array of integers. Or an array of characters. Both of which have the values {1,2,3,4}.

An array of strings would look like:

char *something[4] = {"apple", "banana", "carnage", "delicious"};
1 Like

I can see how that corrects my question, but not whether it answers it.
(I'm not being facetious!)

Okay then.

An array of 4 ints takes up more RAM than an array of 4 chars.

If the answer is still not satisfactory, ask a better question.

Thanks for your help. I should have known better than to ask the question.

False. Read about arduino data types.

Neither of the above contain an array of characters.

// int apple, banana, carnage, delicious;
// int something[4] = { apple, banana, carnage, delicious };  // four x 16 bits

// char apple, banana, carnage, delicious;
// char something[4] = {apple, banana, carnage, delicious}; // four x 8 bits

char something[][9] = {"apple\0", "banana\0", "carnage\0", "delicious\0"}; // four x 8 x (numchars + 1)

void setup() {
  Serial.begin(115200);
  for (int i = 0; i < 4; i++)
    Serial.println(something[i]);
}

void loop() {}

with array of ints

Sketch uses 1766 bytes (5%) of program storage space. Maximum is 30720 bytes.
Global variables use 196 bytes (9%) of dynamic memory, leaving 1852 bytes for local variables. Maximum is 2048 bytes.

With array of chars

Sketch uses 1492 bytes (4%) of program storage space. Maximum is 30720 bytes.
Global variables use 192 bytes (9%) of dynamic memory, leaving 1856 bytes for local variables. Maximum is 2048 bytes.

With array of characters:

Sketch uses 1534 bytes (4%) of program storage space. Maximum is 30720 bytes.
Global variables use 224 bytes (10%) of dynamic memory, leaving 1824 bytes for local variables. Maximum is 2048 bytes.

This is exactly an array of four characters:

The characters contained in that array go by the names SOH, STX, ETX and EOT.

a7

Ummmm. 4th string needs space for 9 chars and the null.

I see my understanding lacks. I call that an array of type char(acters).

... aaand I can not count. Time for me to shovel some rocks.

1 Like

I wasn't going to mention it, but we might leave our OP thinking the \0 was implicit. Whole threads have lived and died around that assumption!