0

I have this code:

int m, n;
cin>>m>>n;
double A[m][n];
//read values for A

How can I pass A to a function?

1 Answer 1

2

Variable length arrays are a C feature and not supported in standard C++. However some compilers such as gcc support it as an extension.

On those compilers that do support it, you would need to pass the array dimensions as parameters and use those parameters as the array size:

void handle_array(int m, int n, double A[m][n])
{
    ...
}

Note that this is only required for a multidimensional array, and not for the first dimension. You could also pass it as follows:

void handle_array(int m, int n, double A[][n])
{
    ...
}

Or equivalently:

void handle_array(int m, int n, double (*A)[n])
{
    ...
}
Sign up to request clarification or add additional context in comments.

2 Comments

I feel like this leaves some information out, like how m is the only dimension that needs to be passed in separately, and that n must be specified when writing the array parameter but not m. More importantly, it leaves out a more idiomatic C++ approach that doesn't rely on a compiler extension.
@sweenish Yes, added that in.

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.