Need some help passing arrays

I've got a program where i'm trying to pass an array to a function, then print an element from that passed array. It only prints element 0.

I believe it has to do with pointers--which i really don't understand.

The attached code prints this (notice the missing element 1

start Black White Up Down Sensor Source End Black

Up

Sensor

Any help greatly appreciated.

const char *InvertText[2][3] = {{"Black"}, {"White"}};
const char *OrientationText[2][4] = {{"Up"}, {"Down"}};
const char *VoltInputText[2][6] = {{"Sensor"}, {"Source"}};


void setup() {

  Serial.begin(9600);

  delay(1000);
  // this works
  Serial.println("start");
  Serial.println(*InvertText[0]);
  Serial.println(*InvertText[1]);
  Serial.println(*OrientationText[0]);
  Serial.println(*OrientationText[1]);
  Serial.println(*VoltInputText[0]);
  Serial.println(*VoltInputText[1]);
  Serial.println("End");

}

  void loop() {

    DumpText(*InvertText);
    DumpText(*OrientationText);
    DumpText(*VoltInputText);
    while(1){}
  }

  void DumpText(const char *myarray[]) {

    // only element 0 prints
    Serial.println(myarray[0]);
    Serial.println(myarray[1]);
   

  }
const char *InvertText[2][3] = {{"Black"}, {"White"}};
const char *OrientationText[2][4] = {{"Up"}, {"Down"}};
const char *VoltInputText[2][6] = {{"Sensor"}, {"Source"}};

You have two dimensional arrays of pointers. Why? You only use one dimension.

  Serial.println("start");
  Serial.println(*InvertText[0]);
  Serial.println(*InvertText[1]);
  Serial.println(*OrientationText[0]);
  Serial.println(*OrientationText[1]);
  Serial.println(*VoltInputText[0]);
  Serial.println(*VoltInputText[1]);
  Serial.println("End");

You are printing the 2D arrays as though they were 1D arrays. Why?

it is easier than you made it...

char* InvertText[] = {"Black", "White"};  // an array of pointers to char arrays
char* OrientationText[] = {"Up", "Down"};
char* VoltInputText[] = {"Sensor", "Source"};

void setup()
{
  Serial.begin(9600);

  delay(1000);
  Serial.println("start\n");
  Serial.println(InvertText[0]);
  Serial.println(InvertText[1]);
  Serial.println("End\n");
}

void loop()
{
  DumpText(InvertText);
  DumpText(OrientationText);
  DumpText(VoltInputText);
  while (1) {}
}

void DumpText(char** myarray)  // weird, huh... a pointer to a pointer to a char (array)!!
{
  Serial.println(myarray[0]);
  Serial.println(myarray[1]);
}

@Bulldog: Given the way you've used it, would it be easier for a beginner to understand using this signature:

void DumpText(char* myarray[])

terms like better and easier are subjective.

econjack: @Bulldog: Given the way you've used it, would it be easier for a beginner to understand using this signature:

void DumpText(char* myarray[])

It's your prerogative to believe that but I think the other syntax is easier to grasp, it is a pointer to a pointer. Beginner needs to also be schooled that a passing and array name like that is simply resolving to a constant pointer.

KrisKasprzak: I believe it has to do with pointers--which i really don't understand.

Thanks bulldog, that did the trick

BulldogLowell:
terms like better and easier are subjective.

It’s your prerogative to believe that but I think the other syntax is easier to grasp, it is a pointer to a pointer. Beginner needs to also be schooled that a passing and array name like that is simply resolving to a constant pointer.

Agreed, but if you think that, then why didn’t you write the function as:

void DumpText(char** myarray)  // weird, huh... a pointer to a pointer to a char (array)!!
{
  Serial.println(*myarray);
  Serial.println(*(myarray + 1));
}

True, it is subjective, but I’ve discussed this very topic with a number of my students and their feeling is that an array of pointers to char is more clear than a pointer to a pointer, especially when used with string data . I think the version above is harder to explain than your first version. I think you do, too, or you would have written it as above to show consistency with the function signature.