1

I have two multidimensional arrays

uint8_t arr1 [24][8];
uint8_t arr2 [24][8];

I am writing a function to print out the contents of these arrays, and I wish to specify to the function which of the arrays I want printed. I tried the following:

void print_array(int n) {
  uint8_t arr[24][8];

  if (n == 1) {
    arr = arr1;
  }
  else if (n == 2) {
    arr = arr2;
  }

  // ... code to print "arr" contents ...
}

Basically, I want to be able to copy the reference to the multidimensional array to avoid having duplicated code to print the array contents. The above gives me an 'invalid array assignment' error. What do I need to do to copy the array reference successfully?

1
  • You can't just assign the elements of an array with the name of the array. You need to copy the data as well with memcpy or such Commented Mar 23, 2014 at 12:58

3 Answers 3

4

When an array is used as a value (I'm talking of C arrays) then its name represents the address of the first element. This means that either you need to copy the memory by hand somehow (memcpy) or that you need to use that pointer somehow.

For the second choice (recommended since there's no copy involved) this will work:

#include <iostream>
using namespace std;

uint8_t arr1 [24][8];
uint8_t arr2 [24][8];

void print_array(int n) {
  uint8_t (*arr)[24][8];

  if (n == 1) {
    arr = &arr1;
  }
  else if (n == 2) {
    arr = &arr2;
  }

  for(int i=0; i<24; i++){
    for(int j=0; j<8; j++)
        printf("\n %d ", (int)(*arr)[i][j]);
  }
}

int main() {

    arr1[0][1] = 3;
    arr1[1][5] = 6;

    print_array(1);
    return 0;
}

Try it live: http://ideone.com/CvMQfB

Also notice that if you're using C++11, then you might get away with std::array and have exactly what you had in mind with the same syntax

std::array<int,4> A = {10,20,30,40};
std::array<int,4> B = A; //copy array A into array B
Sign up to request clarification or add additional context in comments.

4 Comments

It could be copied with uint8_t (*arr)[8]; arr = arr1;
I kindly disagree with that and I invite you to try it out
Ok, I see, it doesn't copy, it's just a reference. Thanks. Is there no way to copy without memcpy?
1) Use C++11's array, 2) Do a for loop and copy every element, 3) Use memcpy knowing the dimensions, 4) Use pointers. Notice that many of the previous are the same, just abstracted.
1

You could create pointer to array:

void print_array(int n) {
  uint8_t (*arr)[24][8];

  if (n == 1) {
    arr = &arr1;
  }
  else if (n == 2) {
    arr = &arr2;
  }

  // ... code to print "arr" contents ...
}

Comments

0

I would do something like :

void print_array(const uint8_t (&arr)[24][8]) {
    for (std::size_t i = 0; i != 24; ++i) {
        for (std::size_t j = 0; j != 8; ++j) {
            std::cout << arr[i][j] << " ";
        }
        std::cout << std::endl;
    }
}

then call it print_array(arr1); or print_array(n == 1 ? arr1 : arr2);.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.