2

I am a little bit confused about how to assign a 2d array pointer parameter to a local 2d array pointer variable. Check the code below.

#define N   5
#define M   6

void print(int (*arr)[M][N]) {

    int *localArr[M][N];

    localArr = arr;  //error C3863: array type 'int *[6][5]' is not assignable
    //localArr[0][0] =1; and so on.
}

int main()
{

    int Array1[M][N];
    print(&Array1);         
}
5
  • 2
    Look at the declared type of arr and declared type of localArr. See any difference? Commented May 6, 2020 at 18:12
  • arr and localArr have different types. arr is a pointer to an array of arrays of int, while localArr is an array or arrays of pointers to int. Commented May 6, 2020 at 18:12
  • Think parenthesis... Commented May 6, 2020 at 18:13
  • Thanks, any other optimum way to do this? Speeding is critical in my case. Commented May 6, 2020 at 18:23
  • @Someprogrammerdude can you sketch the two arrays to visualize them? I am confused. Commented May 6, 2020 at 18:31

3 Answers 3

1

the local declaration has to be like this way,

 int (*localArr)[M][N]; //pointer to an MxN array
 //int * localArr[m][N];//An MxN array of pointer to int
Sign up to request clarification or add additional context in comments.

2 Comments

Hi thanks, i will check it tomorrow. Can you suggest an easier or optimal way to pass a 2d array to a local function variable? I don't like the idea with offsets calculations..
I think pointer to array is the right approach if you know the exact size(M & N) because the compiler knows the size and catch errors if any during compile time.
0

What do you want to achieve? If you just want to print your 2d array then why don't you use this approach?

void print(int localArr[M][N]) {

    for (int i = 0; i < M; i++) {
        for (int j = 0; j < N; j++) {
            cout << localArr[i][j];
        }
    }
}

If there are some constraints then Nitheesh is right!

1 Comment

localArr is actually a class variable that is used from many methods.
0

If we change the sizes to make it easier to fit:

#define M 2
#define N 3

Then somewhat graphically the variable arr is something like this:

+-----+      +--------+--------+--------+--------+--------+--------+
| arr | ---> | [0][0] | [0][1] | [0][2] | [1][0] | [1][1] | [1][2] |
+-----+      +--------+--------+--------+--------+--------+--------+

arr is a pointer. It points to an array of arrays, where each element is an int.

That means you can dereference arr to get what it points to, and then use array indexing on that to get an int value:

int value = (*arr)[0][1];

Now if we take localArr instead, it's more like

+----------------+----------------+----------------+----------------+----------------+----------------+
| localArr[0][0] | localArr[0][1] | localArr[0][2] | localArr[1][0] | localArr[1][1] | localArr[1][2] |
+----------------+----------------+----------------+----------------+----------------+----------------+

localArr is an array of arrays, where each element is a int *.

So you need an int * to get a single element:

int *pointer = localArr[0][1];

3 Comments

Thanks man, I will play with them tommorow, to find what is best for my needs.
What I really wanted from the beginning is to pass the memory address (pointer) of the array. So, I believe, the arr declaration is the right choice as NitheeshGeorge suggested.
@Maverick Personally I would rather use std::array instead, and if possible use references instead of pointers.

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.