Do I need to put “&” when I pass a 2D array to a function or 2D arrays automatically do so by reference as 1Ds.
void fnc (int& arr[5][5]) {
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It will be passed by value if you don’t specify pass by reference
&.However arrays decay to pointers, so you’re basically passing a pointer by value, meaning the memory it points to will be the same.
In common terms, modifying
arrinside the function will modify the originalarr(a copy is not created).Also, 1D arrays also aren’t passed “automatically” by reference, it just appears so since they decay to pointers.