2d array of number strings

I want to create a 2d array of strings, probably numbers. They will only be accessed in full, no need for access to individual characters from the strings.

There will be 40 of them and they will all be 15 characters in length.

I seem to be able to create them using char* hist[20][15] = {"100000000000000","200000000000000" ,"300000000000000","400000000000000" ,"500000000000000","600000000000000" ,"700000000000000","800000000000000" ,"900000000000000","100000000000000" ,"110000000000000","120000000000000" ,"130000000000000","140000000000000" ,"150000000000000","160000000000000" ,"170000000000000","180000000000000" ,"190000000000000","200000000000000" };

I am having trouble in accessing them them.

If I Serial.println(hist[12]); It reports an error.

Actually I remember that I should actually create the array like this maybe: char* hist[] = {"100000000000000","200000000000000" ,"300000000000000","400000000000000" ,"500000000000000","600000000000000" ,"700000000000000","800000000000000" ,"900000000000000","100000000000000" ,"110000000000000","120000000000000" ,"130000000000000","140000000000000" ,"150000000000000","160000000000000" ,"170000000000000","180000000000000" ,"190000000000000","200000000000000" };

Then I can access them with Serial.println(hist[12]);

Will this array be updatable during program runtime?

Is this type of array of strings bad practice?

And that trouble is. . .?

That's a 1d array of strings. A 2d array of characters, but only a 1d array of strings. Remember, a string is an array of characters.

So exactly what is it you want to create?

Please post all the code, and the error message, using code tags.

This, array. Which I obviuosly have already created. So, as I asked, I want to know if this is changable during runtime?

Oh, and I also asked if it is a good method. I'm pretty sure all of this is in the edited original post...

No, and it is not legal C/C++ unless the array data are declared "const".

Some compilers let you off with a warning, but this is very dangerous because during runtime, you can easily write outside of array boundaries.

What is a good method to create an array of many (40) strings each of 13 characters, which is editable during runtime?

char strings [40][14];

char strings[40][14] = {0};

Since the ones in your example are so simple and mostly ASCII zeros, I would initialize the data at runtime using snprintf(), etc.

When I use: char hist[20][14] = {"100000000000000","200000000000000" ,"300000000000000","400000000000000" ,"500000000000000","600000000000000" ,"700000000000000","800000000000000" ,"900000000000000","100000000000000" ,"110000000000000","120000000000000" ,"130000000000000","140000000000000" ,"150000000000000","160000000000000" ,"170000000000000","180000000000000" ,"190000000000000","200000000000000" };

Works fine, then I print a couple with: Serial.println(hist[12-1]); // print 12th Serial.println(hist[5-1]); // print 5th that works fine too.

But when I try to edit the 5th, it doesn't print anything after I print it:
char hist[5-1]={"1234567891011"}; Serial.println(hist[5-1]); this only adds blank lines to the Serial Monitor.

That creates a new variable called "hist", with 4 elements, and tries to initialise it with far too many characters.

Use strcpy.

Thanks for taking the time to reply and for trying to help. I have never used strcpy.

Can you show me how I should update the contents of the 5th element of the string array I have created? Using strcpy, if that is a good method?

strcpy (hist [4], "foobar");
1 Like

If

char *hist[20][15];

is a 1d array of strings, how does one declare a 2d array of pointers to char?

a7

Sorry, upon trying this, my lack of understanding of Strings strings and chars is obviously getting in the way.

I tried to update the single element of the array with strcpy(hist[amount],bla); and it reports "cannot convert 'String' to 'const char*' for argument '2' to 'char* strcpy(char*, const char*)' "

bla is a variable I created with String bla = "1234567891011"

Is there a way to use the string I have or should I be defining the String differently initially?

Again, sorry, this language with 3 ways to deal with strings and characters is quite confusing for me compared to the other languages I have used.

I hardly ever use Strings, but there should be a class function like "c_str" that would allow you to "convert" "bla" to a string.
Check the language reference for the String class.

Better still, don't try to mix Strings and strings.

(I don't know any language that doesn't draw a distinction between single characters and strings of characters)

Actually, you should avoid using String objects with Arduino. They cause memory problems and program crashes.

I have been reading that a lot. How would I define the initial bla to work seamlessly in place of the number in quotation marks here then: String bla = "1234567891011" So that it works with strcpy(hist[amount],bla); ?

Just don't try to put more than 13 characters into bla, which is a C-string (zero terminated character array).

Any of the languages I have used have allowed left(), mid() and right() to select a single character or many characters of a string and dealt with them all as strings or at least as the same type of thing. I have been getting away with doing that for years.