Printing multidimensional array

How do I print a multi dimensional array in Arduino?

I wanted to create a function that accepts a void pointer and supply it with different sizes of multidimensional arrays.

#include <Arduino.h>

// Steps
struct StepData{
  char command;
  int position;
};


const int step1_rows = 3;
const int step1_cols = 2;
StepData step1_sequence[step1_rows][step1_cols] = {{'1', 200}, {'2', 300}, {'3', 400}};

void *active_step_sequence;

void printMultidimensionalArray(void* voidPointer, int rows, int cols) {
  // Iterate through the void pointer to print the values
  for (int i = 0; i < rows; i++) {
    for (int j = 0; j < cols; j++) {
      StepData* currentData = reinterpret_cast<StepData*>(static_cast<byte*>(voidPointer) + sizeof(StepData) * (i * cols + j));
      
      Serial.print("Char: ");
      Serial.print(currentData->command);
      Serial.print("\tInt: ");
      Serial.println(currentData->position);
    }
  }
}

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

  Serial.println("Begin program...");
}

void loop()
{

  active_step_sequence = step1_sequence;

  static bool isDone = false;

  if (!isDone)
  {
    isDone = true;
    printMultidimensionalArray(static_cast<void*>(active_step_sequence), step1_rows, step1_cols);
  }
}

The output:

Begin program...
Char: 1 Int: 200
Char: ␀ Int: 0
Char: 2 Int: 300
Char: ␀ Int: 0
Char: 3 Int: 400
Char: ␀ Int: 0

Not sure why there is a null value when it prints?
I am no expert in C++ and I got these codes from surfing the internet and other AI tools.

Sorry, do you mean like this?

const int step1_rows = 3;
const int step1_cols = 2;
StepData step1_sequence[step1_rows][step1_cols] = {{'1', 200}, {'2', 300}, {'3', 400}, {'A', 200}, {'B', 300}, {'C', 400}};

I am getting an error "Too many too many initializer values" if I do it like this.

there are 2 StepData entries per row. Each StepData entry has 2 fields
corrected - but simply increments a pointer without knowing the dimensions of the matrix

output

Begin
 1  200, 2  300,
 3  300, 9  100,
 4  150, 7   30,

#include <Arduino.h>

// Steps
struct StepData{
    char command;
    int position;
};

const int step1_rows = 3;
const int step1_cols = 2;
StepData step1_sequence [step1_rows][step1_cols] = {
    { {'1', 200}, {'2', 300} },
    { {'3', 300}, {'9', 100} },
    { {'4', 150}, {'7',  30} },
};

char s [90];

// -----------------------------------------------------------------------------
void printMultidimensionalArray (
    void *voidPointer,
    int   rows,
    int   cols)
{
    StepData *p = (StepData *) voidPointer;

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            sprintf (s, " %c %4d,", p->command, p->position);
            Serial.print (s);
            p++;
        }
        Serial.println ();
    }
}

// -----------------------------------------------------------------------------
void setup ()
{
    Serial.begin (115200);
    Serial.println ("Begin");

    printMultidimensionalArray (step1_sequence, step1_rows, step1_cols);
}

void loop ()
{
}

It has been discussed here many times that doing it that way is not allowed by the C++ standard and invokes undefined behavior. See here.
One alternative is to use a templated function.

You need to format the initialisation to reflect the fact that you are loading a two dimensional array.

const int step1_rows = 3;
const int step1_cols = 2;

StepData step1_sequence[step1_rows][step1_cols] = {
  {{'1', 200}, {'2', 300}}, 
  {{'3', 400}, {'4', 200}}, 
  {{'B', 300}, {'C', 400}}, 
};

Three [3] rows each of which is [2] columns each of which has 2 parts, namely the values of the two struct components.

Your original code, with that change, functions.

On the other hand, as you have been shown, it is crap-full of C++ nonsense mysteries you have no business copy/pasting from an AI, particularly since the task can be accomplish with much plainer code.

AI - good for what it is good for, different for each who seeks to exploit it. Leave it on the side for now, it is not a useful tool for you.

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.