0

I have an array of arrays and I need to pass it to a function. How would I do this?

int mask[3][3] = {{1,2,3},{4,5,6},{7,8,9}}; 

How would the function signature look like?

I thought it would look like this

void foo(int mask[3][3])

or

void foo(int **mask)

but it seems that I am wrong and I couldn't find an example.

1
  • At least tell what you think it would look like. We can help you from there. Commented Dec 3, 2013 at 23:57

1 Answer 1

3

The least surprising way to pass arrays in C++ is by reference:

void foo(int (&mask)[3][3]);

Your first try

void foo(int mask[3][3])

should have worked, but it would be internally converted to

void foo(int (*mask)[3])

with unexpected implications for sizeof and other things that need a "real" array.

Your other idea int** doesn't work, because a 2-D dimensional array is not the same as an array of pointers, even though the a[i][j] notation works equally well for both.

If you don't want to share the content with the function (and see changes it might make), then none of the above will help. Passing a pointer by value still shares the target object. In that case you need to put the array inside another object, for example a std::array<std::array<int,3>,3>

Sign up to request clarification or add additional context in comments.

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.