An Array or Arrays

So after much googling i cant find a solution…

I need want to make an array or arrays to use in wire.write()

Is it possible to do the following?

char myArray = {‘array1’,‘array2’,‘array3’};

char array1 = “figure1”;
char array2 = “figure2”;

and so on…

Also, is it possible to use a decimal number “7.00” (e.g. char array1 = “7.00”) in an array? or would it need to be written differently?

Look up double-dimensioned arrays or arrays of rank 2. What you need is something like:

char myArray[10][25];

which gives you ten elements, each of which can hold up to 25 characters. Try:

void setup() {
  // put your setup code here, to run once:
  char myArray[10][25] = {"Figure 1", "Figure 2", "Figure 3"};

  Serial.begin(115200);
  for (int i = 0; i < 10; i++) {
    Serial.println(myArray[i]);
  }
}

void loop() {
  // put your main code here, to run repeatedly:

}

I believe it would look like this

char myarray[numstrings][stringsize + 1] = {"Apple","ball","etc"};

Likely you could Char myarray[][] = {"a","b"};

The array1 and array2 things are not how you would address the single array you made though. In the first example, myarray[0] would equal "apple\0".

As for the decimal number thing, it would be a literal "7.00" but not the 7 (unless you convert it with atoi() Orr something similar.

Personal preference only, it compiles in essentially the same way, but in this case I would go with an array of pointers to strings:

const char *myArray[] = {
  "Figure 1",
  "Figure 2",
  "Figure 3"
};

That way, you would reference each element, for example:

Serial.println(myArray[0]);

Again, the difference is mostly conceptual.

Dealing with strings is a bit tricky.

First of all, there is a function called strcpy that is used for copying strings. More details here: http://www.cplusplus.com/reference/cstring/strcpy/

Second, when dealing with a two-dimensional character array, as you are doing, you have to know how to deal with the array indices. More details here: http://stackoverflow.com/questions/2966644/assigning-a-string-to-a-2d-array

gfvalvo: Personal preference only, it compiles in essentially the same way, but in this case I would go with an array of pointers to strings:

const char *myArray[] = {
  "Figure 1",
  "Figure 2",
  "Figure 3"
};

That way, you would reference each element, for example:

Serial.println(myArray[0]);

Again, the difference is mostly conceptual.

The difference is a lot more than conceptual if the strings are not all of equal length. Using pointers can greatly reduce RAM usage, and also allows the array contents to be changed by changing a single pointer, rather than copying an entire string.

Regards, Ray L.

Agreed, thanks. I was only thinking in terms of the variable type and how the pointers de-reference.

2D array (3x3) … use bounds for all dimensions except the first.

void setup() {

  char* myArray[][3] = {
    {"figure1", "Figure A: ", "Figure 7.00"},
    {"figure2", "Figure B: ", "Figure 5.76"},
    {"figure 3.0", "Universal Synchronous Asynchronous Receiver Transmitter", "Revision History"},
  };

  Serial.begin(115200);

  // print columns
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      Serial.println(myArray[j][i]);
    }
  }
}

void loop() {}

is it possible to use sprintf?

i came across this after after a little more digging http://stackoverflow.com/questions/5889880/better-way-to-concatenate-multiple-strings-in-c

is it possible to use sprintf?

What happened when you tried it ?