passing two dimensional array to function

Hey folks :slight_smile:

Sorry to bother you, but i tried to tackle these problems for three days already and i am pretty lost. The end goal was a function that compares two two dimensional arrays, which are the old and the new state of a x by y led matrix.

  1. First problem narrowed down:
const int rows = 4;
const int cols = 3;
int pixels[rows][cols] = {HIGH};

void setup() {
}

void loop() {
  zele(pixels);
}

void zele(int pixels[][cols]) {
  pixels[0][0] = LOW;
}

Verifying this code returns ‘error: ‘cols’ was not declared in this scope’. I am not happy with this as http://arduino.cc/en/Tutorial/Variables says ‘For example, if you want to be able to use a variable anywhere in your program, you can declare [the variable?] at the top of your code.’
Question 1: Why Arduino doesn’t like my variable? How should i pass the cols without using magic number? Replacing ‘void zele(int pixels[cols]) {’ with ‘void zele(int pixels[3]) {’ works.

  1. This
const int rows = 2;
const int cols = 1;

int pixels[rows][cols] = {HIGH};

void setup() {
  Serial.begin(9600);
  Serial.println("-----");
  for (int m = 0; m < rows; m++) {
    for (int n = 0; n < cols; n++) {
      Serial.print(m);
      Serial.print(n);
      if (pixels[m][n] == HIGH) {
        Serial.println("HIGH");
      }
      else {
        Serial.println("LOW");
      }
    }
  }
}

void loop() {
}

prints

00HIGH
10LOW
but i expect they both to be HIGH.
Question 2. Why ‘int pixels[rows][cols] = {HIGH};’ doesn’t initialize all the matrix with HIGHs? Is the first answer here http://stackoverflow.com/questions/3280203/how-to-initialize-three-dimensional-array-in-arduino wrong? Am i missing something?

  1. After some unsuccessful tries i decided to go for dynamic size arrays as per third answer here Multidimensional Dynamic Memory Arrays - C++ Forum
const int rows = 4;
const int cols = 3;

// declaration
int ** pixels;

void setup() {
  // allocation
  pixels = new int*[rows];
  for(int i = 0; i < rows; i++)
    pixels[i] = new int[cols];
  // initialization
  for(int j = 0; j < rows; j++)
    for(int i = 0; i < rows; i++)
      pixels[i][j] = HIGH;
}

void loop() {
  zele(pixels);
}

void zele(int ** pixels) {
  pixels[0][0] = LOW;
}

but got ‘undefined reference to `operator new(unsigned int)’’. The answer here http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1277398462 is ‘There is no “new” on the Arduino’, so i decided to create it http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=59453

#include <stdlib.h> 

void * operator new(size_t size); 
void operator delete(void * ptr); 


void * operator new(size_t size) 
{ 
  return malloc(size); 
} 

void operator delete(void * ptr) 
{ 
  free(ptr); 
} 

const int rows = 4;
const int cols = 3;

// declaration
int ** pixels;

void setup() {
  // allocation
  pixels = new int*[rows];
  for(int i = 0; i < rows; i++)
    pixels[i] = new int[cols];
  // initialization
  for(int j = 0; j < rows; j++)
    for(int i = 0; i < rows; i++)
      pixels[i][j] = HIGH;
}

void loop() {
  zele(pixels);
}

void zele(int ** pixels) {
  pixels[0][0] = LOW;
}

but got the same error.
Question 3: How can i create x by y not-so-dynamic size array?

At the end i just removed the arrays and got the sketch working, so for me the problem is solved, but i am still curious :slight_smile:

Thank you in advance,

kartofius

Verifying this code returns 'error: ‘cols’ was not declared in this scope'.

It actually returns a message with a lot more information than that, such as the row number that the problem occurs on.

I'm not sure why you think you need to pass a global variable to a function, anyway.

  1. This

prints

00HIGH 10LOW but i expect they both to be HIGH.

I can't imagine why, since you are only providing one initializer for your 12 element array. The rest will be initialized to the default value for the type, which, in the case of int, is 0 (or LOW).

  pixels = new int*[rows];

Given your implementation of new, this will allocate 4 bytes, which is nowhere near enough space for 4 int pointers.

    pixels[i] = new int[cols];

Given your implementation of new, this will allocate 4 bytes, which is nowhere near enough space for 4 ints.

Due to some peculiarities of the IDE, the new and delete operators need to be declared and implemented in a header file that is included in the sketch.

Why do you need to pass the array? you have declared pixels as a global variable

const int rows = 4;
const int cols = 3;

int pixels[rows][cols];
void setup() {
  for(int i = 0; i < rows; i++){
    for(int j = 0; j < cols; j++){
      pixels[i][j] = HIGH;      
    }
  }
}

void loop() {
  zele();
}

void zele() {
  pixels[0][0] = LOW;
}

Also, you can’t initialise an array like that - see the setup() for how to do it.