2D Arrays | Sololearn: Learn to code for FREE!


2D Arrays

Why do we have to write columns of an array to a function parameter? If we pass columns why do we not pass rows of the array to the function? And what happens when I pass an array as a parameter to a function? Do the function take a copy of the array or ..?

11/22/2017 7:28:00 PM


3 Answers

New Answer


Passing an array in C/C++ is the same as passing the pointer. In that what happens (as @Baptiste points out) is a copy of the memory location of the array is passed into the function, not the actual array. This is exactly what a pointer is (in essence). If we really want to get down to semantics, there's no such thing as an array in C. All an array is by nature is a location in memory where one or more items of the array type are stored. So int arr[10] is the same as having a int pointer point to the location of 10 integers.


It does not make a copy of the array, it makes a copy of the address of the array To pass arrays (let's say of int), you can do like that : void foo(int ** matrix, size_t rows, size_t columns); PS : size_t is equivalent to unsigned long


Take a look at this https://code.sololearn.com/cZhx8kDf11rQ/?ref=app