Is mtx_type Pippo[2][3] ; the same as mtx_type Pippo[6] ; ?

I'm looking at the matrix library, and I have a question about declaring matrices.

Inside the library the matrix is treated as a single "row" of numbers.

But I also see that the Arduino compiler accepts

mtx_type Pippo[2][3] ;

as well as

mtx_type Poppi[6] ;

Are they the identical as far as the compiler and matrix library are concerned?

And is there any practical difference in using in a global variable or in a local variable? If you see what I mean.

there are identical in terms of size

given an Nrow x Ncol, it's typically more convenient to locate an element in an array as A[row][col] versus A [(row*Ncol) + col].

And while the size of each dimension of an array is used to determine the amount of memory to allocate for the array, those sizes are not checked at run time to prevent indexing beyond the limits of the array.

Ok, thanks for the reply. I think the problem is when I pass mtx_type* into a function... but I'll get therfe in the end!

if you use an array in the same file is is defined in, the size(s) are know.

if you pass an array to a different file, you need to pass the sizes and i'm not sure you can use multidimensional arrays.

gcjr:
…i’m not sure you can use multidimensional arrays.

There’s probably a way to do it with that a C++ guru can suggest. But, you can also do it with old-school C:

void printArray(uint8_t *ptr, uint8_t nRows, uint8_t nCols);

void setup() {
  uint8_t myArray[2][3] = {
    {1, 2, 3},
    {4, 5, 6}
  };

  Serial.begin(115200);
  delay(1000);

  printArray(&myArray[0][0], 2, 3);

}

void loop() {
}

void printArray(uint8_t *ptr, uint8_t nRows, uint8_t nCols) {
  for (uint8_t i = 0; i < nRows; i++) {
    for (uint8_t j = 0; j < nCols; j++) {
      Serial.print(*(ptr + i * nCols + j));
      Serial.print(" ");
    }
    Serial.println();
  }
}

gcjr:
if you use an array in the same file is is defined in, the size(s) are known.

Not necessarily, I may have many matrices of different sizes, all in the same file :frowning:

gfvalvo:

void printArray(uint8_t *ptr, uint8_t nRows, uint8_t nCols);

void setup() {
 uint8_t myArray[2][3] = {
   {1, 2, 3},
   {4, 5, 6}
 };

Serial.begin(115200);
 delay(1000);

printArray(&myArray[0][0], 2, 3);

}




Since arrays are always passed as pointers, do you need the "&myArray[0][0]". 

I think, but I have not checked, that "myArray" would be sufficient...

giovanniguerra:
Not necessarily, I may have many matrices of different sizes, all in the same file :frowning:

but within that file, the dimensions of each array are known

i’m a fan of statically initialized tables. the problem is accessing the table from another file w/o knowing its size.

a common approach for determine the size of a table is – #define TableSize sizeof(table)/sizeof(Table_type).

so i’ve also initializes a variable in the file with the table to its size using the #define. Both the table address and size are now known outside the file at run-time.

giovanniguerra:
I think, but I have not checked, that "myArray" would be sufficient...

Turn up your compiler warning level and try it. 'myArray' will be passed as a 'char **'. The function expects a 'char *'.

gfvalvo:
Turn up your compiler warning level and try it. 'myArray' will be passed as a 'char **'. The function expects a 'char *'.

Argh! C (and early C++) has always been a bit primitive with arrays!

gcjr:
but within that file, the dimensions of each array are known

Ok. So you mean you pass the dimensions of the array to the functions which handle them?

But you could do the same thing to functions in other files.

giovanniguerra:
Argh! C (and early C++) has always been a bit primitive with arrays!

It's strongly typed. Do it correctly and it works just find. But, it's intolerant of sloppiness.

Hmmm. If it is so strongly typed how come it permits a cast from a 2d array to a 1d array?

"Sloppiness", what are you suggesting? :wink:

IMHO C is a high level language with lots of looseness in it. You can cast anything to anything.

C++ is a bit of a mess.

C# is a marvel!

IMHO!

gfvalvo:
It’s strongly typed. Do it correctly and it works just find. But, it’s intolerant of sloppiness.

It is a strongly typed comparing to C.

But comparing to other modern languages, C++ is considered a weak typed language.

The scale (that I know of) goes like this:

— Aida --------- Java/C# ----------------------------------------------- C++

when i joined the labs, people in my group were using the karmarkar algorithm and APL because it handled matrix operations.

C is designed to efficiently interface to hardware and support operating system features.

We'll have to meet in another forum, we are going wildly off topic here!

But thanks to all who have replied, I've learned something.