-1

I have written a function that takes in a dynamic length array but with fixed inner array size, the second parameter in the function is the length of the parent array. When I try to access the nested values however, I get the issue mentioned above.

void myFunc(int arrOfArr, int arrOfArrLen) {
  // try to access
  arrOfArr[0][1]; // expect val2
}

example usage

myFunc(
  {
    {val1, val2},
    {val3, val4}
  },
  2
);

edit: I realize "contextually" obviously an integer has no indexes, but that's how you declare an array it seems...(truthfully in Arduino context) but apparently it's still C++

Here's a runable demo of above from the first sandbox Google returned

http://cpp.sh/5sp3o

update

I did find a solution, it's ugly but it works:

instead of passing in a "raw" nested array as a param, I set it as a variable first eg:

int arrOfArr[][3] = {
  {val1, val2},
  {val3, val4}
}

Then in the function I do the same thing

void myFunc(int arrOfArr[][3], int arrOfLen) {
  // access
}

Call it

myFunc(arrOfArr, 2);

As I said it's ugly but works for me, this is a passing project thing not a low-level dev, maybe will learn it fully later on but not needed in day job.

edit: apparently the thing I was trying to do initially eg. embed an initializer list as a param does not work.

it_is_written
  • 43
  • 1
  • 7
  • 1
    Hint: `int` is not an array type. You need to be more specific in your function signature. You should also use `std::vector` if possible. – tadman Aug 21 '20 at 23:43
  • 2
    Does this answer your question? [Passing Arrays to Function in C++](https://stackoverflow.com/questions/14309136/passing-arrays-to-function-in-c) – Jan Schultke Aug 21 '20 at 23:43
  • @tadman, yeah maybe I should have posted in the Arduino forum instead, but they declare arrays with the int in front – it_is_written Aug 21 '20 at 23:46
  • @J.Schultke I'm reading up on it, thanks for the link – it_is_written Aug 21 '20 at 23:46
  • 1
    If it's C++ then `int` alone is not sufficient. Maybe you mean `int*`? In that case you have a 1D array though, not 2D like you want. – tadman Aug 21 '20 at 23:47
  • that did change something(ha), but I have a few issues "int[int] for array subscript" and also "no matching function for call to myFunc..." ahh man I've been in the high level language world taken it for granted – it_is_written Aug 21 '20 at 23:52
  • You would have to use `int**` for a 2D array – Remy Lebeau Aug 22 '20 at 00:00

1 Answers1

1

if you want to pass a nested array, the declaration may be:

template<size_t N>
void myFunc(int const arrOfArr[][N], int arrOfArrLen) {
  // ...
}

and you can remove the template argument if N is already decided.

const size_t N = 3;
void myFunc(int const arrOfArr[][N], int arrOfArrLen) {
  // ...
}

but it doesn't work if you pass a brace-enclosed initializer, you can add a overloaded function:

template<size_t M, size_t N>
void myFunc(int const (&arrOfArr)[M][N], int arrOfArrLen){
  // attention: int *const*
  // ...
}
RedFog
  • 1,005
  • 4
  • 10
  • Interesting, in my frantic Googling, I did find the template stuff and the "overloaded" thing(didn't know that's what it's called), how do you pass in the value? In my solution it's verbose where you have to define an int before passing it into the param call. – it_is_written Aug 22 '20 at 02:36