I'm working on an Arduino project where I need to build (and work with) a two-dimensional array at runtime. I've been poking around looking for a solution, but I've had no luck. I found an example of a dynamic one-dimentional array helper here: http://playground.arduino.cc/Code/DynamicArrayHelper, so i've been trying to adopt that code for my use. I created a library using the following code:
My Header file:
#ifndef Dynamic2DArray_h
#define Dynamic2DArray_h
#include "Arduino.h"
class Dynamic2DArray
{
  public:
    Dynamic2DArray( bool sorted );
    //Add an integer pair to the array
    bool add( int v1, int v2);  
    //Clear out (empty) the array
    bool clear();
    //Get the array item in the specified row, column
    int getValue(int row, int col);    
    //Get the number of rows in the array
    int length();    
  private:
    int _rows;
    void * _slots;
    bool _sorted;
    void _sort();
};
#endif
The library's code:
#include "Arduino.h"
#include "Dynamic2DArray.h"
#define ARRAY_COLUMNS 2
int _rows;
void * _slots;
bool _sorted;
Dynamic2DArray::Dynamic2DArray(bool sorted) {
  //Set our local value indicating where we're supposed to
  //sort or not
  _sorted = sorted;
  //Initialize the row count so it starts at zero
  _rows = 0;
}
bool Dynamic2DArray::add( int v1, int v2) {
  //Add the values to the array
  //implementation adapted from http://playground.arduino.cc/Code/DynamicArrayHelper
  //Allocate memory based on the size of the current array rows plus one (the new row)
  int elementSize = sizeof(int) * ARRAY_COLUMNS;
  //calculate how much memory the current array is using
  int currentBufferSize = elementSize * _rows;
  //calculate how much memory the new array will use
  int newBufferSize = elementSize * (_rows + 1);
  //allocate memory for the new array (which should be bigger than the old one)
  void * newArray = malloc ( newBufferSize );
  //Does newArray not point to something (a memory address)?
  if (newArray == 0)   {
    //Then malloc failed, so return false
    return false;
  }
  // copy the data from the old array, to the new array
  for (int idx = 0; idx < currentBufferSize ; idx++)
  {
    ((byte*)newArray)[idx] = ((byte *)_slots)[idx];
  }
  // free the original array
  if (_slots != NULL)
  {
    free(_slots);
  }
  // clear the newly allocated memory space (the new row)
  for (int idx = currentBufferSize; idx < newBufferSize; idx++)
  {
    ((byte *)newArray)[idx] = 0;
  }
  // Store the number of rows the memory is allocated for
  _rows = ++_rows;
  // set the array to the newly created array
  _slots = newArray;
  //Free up the memory used by the new array
  free(newArray);
  //If the array's supposed to be sorted,
  //then sort it
  if (_sorted) {
    _sort();
  }
  // success
  return true;
};
int Dynamic2DArray::length() {
  return _rows;
};
bool Dynamic2DArray::clear() {
  //Free up the memory allocated to the _slots array
  free(_slots);
  //And zero out the row count
  _rows = 0;
};
int Dynamic2DArray::getValue(int row, int col) {
  //do we have a valid row/col?
  if ((row < _rows) && (col < ARRAY_COLUMNS)) {
    //Return the array value at that row/col
    return _slots[row][col];
  } else {
    //No? Then there's nothing we can do here
    return -1;
  }
};
//Sorted probably doesn't matter, I can probably ignore this one
void _sort() {
}
The initial assignment of the _slots value is giving me problems, I don't know how to define it so this code builds. The _slots variable is supposed to point to the dynamic array, but I've got it wrong.
When I try to compile the code into my project's code, I get the following:
Arduino: 1.8.0 (Windows 10), Board: "Pro Trinket 3V/12MHz (USB)"
sketch\Dynamic2DArray.cpp: In member function 'int Dynamic2DArray::getValue(int, int)':
sketch\Dynamic2DArray.cpp:83:22: warning: pointer of type 'void *' used in arithmetic [-Wpointer-arith]
     return _slots[row][col];
                      ^
Dynamic2DArray.cpp:83: error: 'void*' is not a pointer-to-object type
Can someone please help me fix this code? I've posted the files to https://github.com/johnwargo/Arduino-Dynamic-2D-Array-Lib.
 
     
    