Array Declaration Question

Could someone please help me understand the following line of code:

char buttonlabels[15][5] = {"Send", "Clr", "End", "1", "2", "3", "4", "5", "6", "7", "8", "9", "*", "0", "#" };

As you can see the Array is defined as having two dimensions and is initialized with values.
I do not understand why it needs the second dimension?

All initialized values, in my understanding, are initialized as in a single dimension array.
However, if I remove the second dimension declaration( ie: char buttonlabels[15] ) the compiler simply says that there is not enough room in the array to hold the values stated.

Even if I simply use that line of code in a test sketch using a for loop to step through it, it still needs to be declared as two dimensional. I have read through all the Reference material and more regarding declaring arrays and for the life of me I cannot see what is going on here. :frowning:

This line of code is in an example sketch called phonecal.ino for display on a TFT LCD Touchscreen of a phone keyboard for FONA.

What do I not understand here?

Any help would be much appreciated.

Thanks

DUH!....Ok...it seems that the second definition in the array declaration is the maximum string length of the elements. Even though they are initialized in quotes. The ARDUINO Reference, among others, does not stipulate that!

curly1:
The ARDUINO Reference, among others, does not stipulate that!

It does, actually. It's exactly like this example from the manpage:

char message[6] = "hello";

It's just that you have 15 of them, hence the other parameter required.

Hi Delta_G

Thanks for your reply. Yes I agree it is a wasteful way of going about it. As you say memory is being allocated to hold values that will never be initialized. So why do I need to stipulate the element size for a char Array?
From what I have read if you fill an array with strings..as delineated by using " " then the compiler dynamically sizes the array to hold the string . I mean for instance----

char My Array[3]={" Andrew", " Harry", "Fred"}

Where each value has a different length and the array size is also allowing for the Null character as required.

If I change the declaration of the array from char buttonlabels[15] [5] as in the original code to-

char buttonlabels[15]

The compiler gives the following error msg and exits.

test_array_read:31: error: too many initializers for 'char [15]'

char buttonlabels[15]= {"Send", "Clr", "End", "1", "2", "3", "4", "5", "6", "7", "8", "9", "*", "0", "#" };

exit status 1
too many initializers for 'char [15]'

....Just tried putting pointer " * " after char and increasing array length to 16 as below-

char* buttonlabels[16]= {"Send", "Clr", "End", "1", "2", "3", "4", "5", "6", "7", "8", "9", "*", "0", "#" };

This works but the compiler chucks a whole bunch of warnings about deprecation of string const to char as below.

C:\ARDUINO 8\SKETCHES\test_array_read\test_array_read.ino:2:107: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

char* buttonlabels[16]= {"Send", "Clr", "End", "1", "2", "3", "4", "5", "6", "7", "8", "9", "*", "0", "#" };

^

C:\ARDUINO 8\SKETCHES\test_array_read\test_array_read.ino:2:107: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

.....etc

So I am not sure that this is correct and will definitely produce consistent results.

Can you enlighten me there please?

Best Regards

Derek(curly1...because I have really curly hair :slight_smile: )

char const * const buttonlabels[]
    = {"Send", "Clr", "End", 
        "1", "2", "3", "4", "5", "6", "7", "8", "9", "*", "0", "#" };

Ah! Thanks Coding Badly. I see now(if dimly). As the values are constant ie: The array is initialized to those values, it is indeed a constant but a character constant. No more compile messages!! Thanks heaps. I am now much clearer and feel more assured it will do as I want consistently.
I am only using the phonecal.ino sketch to get my head around the best way of defining buttons, sliders etc.

My head was almost bleeding from all the scratching :0)

Thanks Heaps to all