# Help Understanding Passing Arrays To Functions

• 04-09-2004
jrahhali
Help Understanding Passing Arrays To Functions
When you pass an array to to a function, I read that you don't need to put the size of the array:

Code:

` void somefunc( int elem[ ] );`
I thought about this and it made sense since when you initilize an array you don't need to specify its size, the compiler can figure that out by counting the each element. So in the same way, when the array is passed to the function, it copies each element and the compiler can figure out the size by counting the number of elements that were copied.

Code:

` int somearray[ ] = { 1, 2, 3, 4, 5, 6 };`
So it made sense why you don't need the size when passing an array.
I went on reading about passing multidimenstional arrays to functions. I read that you don't need to specify the size of the first dimenstion but you need to with the last dimenstion:

Code:

` void somefunc( int elem[ ] [5] )`
Now I was lost. Why do you need to specify the size of the last array? The book explained it like this, but i didn't understand it

note: the code that it is reffereing to is
Code:

``` void display( float[DISTRICTS][MONTHS] ) void display( float[][MONTHS] )```
Quote:

Why doesn't the function need this size of the first dimension? Again, remember that a two-dimensional array is an array of arrays. The function first thinks of thE argument as an arrAy of districts. It doesn't need to know how many districts there are, but it does need to know how big each diestrict element is, so it can calculate where a particular element is (by multiplying the bytes per element times the index). So we must tell it the size of each element, which is MONTHS, but not how many there are, which is DISTRICTS.
Can someone please explain this to me?
• 04-09-2004
MrWizard
when you pass an array to a function it really just passes a pointer to the first element of the array. It doesn't need to know how many elements are in that array at all. It doesn't "copy" them over as you suggested. You can easily run off the end of the array just as you could anywhere else. It's your job not to. That's why people generally pass in the size of the array as a parameter. Multi-Deminsional arrays: You don't need to know how many arrays there are just how many elements each array contains. That way when you are accessing different arrays, the compiler can do simple arithmetic pointer operations to skip to the appropriate array.
• 04-09-2004
jrahhali
oh...ok then.
Can someone explain, send a link, or tell me what to search for to find out how the compiler goes about
1) passing primitive data types to functions
2) passing objects/structes to functions
3) passing arrays to functions

I though it was just a matter of copying....i guess that was pretty naive of me.
• 04-09-2004
anonytmouse
OK, say we have an array int arr[6] and an int is two bytes. A graphical layout could look like:
Code:

`|--|--|--|--|--|--|`
Now, you can see to find the value of arr[3] you do not need the size of the array. The calculation is simply:
Code:

`value_address = start_address_of array + (index * sizeof(int))`
Now consider the array int arr[3][5]. If we consider the first dimension to be the rows and the second dimension to be columns the graphical representation is:
Code:

```|--|--|--|--|--| |--|--|--|--|--| |--|--|--|--|--|```
Now, you can see to find the value arr[1][3] we must first go to row 1 and then to column 3. Now, consider if we don't know how many columns there are. In this case, we can not tell where row 0 ends and row 1 starts! The calculation to find a value could be:
Code:

`value_address = start_address_of_array + (number_of_columns * sizeof(int) * row_index) + col_index`
That is why you must provide the size of the second dimension.

Another way to think of this is that the compiler must know the size of each element in an array so that it can find subsequent elements. In the case of int arr[5] each element is an int. In the case of int arr[3][7] each element is an array of 7 ints.

So a generic calculation could be:
Code:

`value_address = start_address_of array + (index * sizeof(element))`
• 04-09-2004
skorman00
Sometimes sizeof does not work inside of a function when an array is passed to it. It's probably compiler specific as is many things. My compiler is MSVC++.net

you can do all that pointer math to figure out where you need to go, but I think it would be much easier to simply pass a second int. All of that math is also depending on the memory being contiguous. If you created your 2d array by first setting up an array of pointers, then allocating those pointers to new arrays, you will wind up going out of bounds.
• 04-09-2004
jrahhali
anonytmouse, thanks a lot, you really cleared things up for me.
Just one more question, (not specifically for anonytmouse). Is it standard to always put the size of the array in the function prototype and declarator when passing them?
• 04-10-2004
Prelude
>Is it standard to always put the size of the array in the function prototype and declarator when passing them?
Where the size can be omitted, it usually is. The first dimension size isn't required, so you'll see functions like this:
Code:

```void foo ( int a[] ); void bar ( char b[][SIZE] );```
• 04-10-2004
jrahhali
cool.
I also read that when you pass an array, the values of the elements are not duplicated, but you actually pass the memory address of the array. I havn't learned anything about memory addressing or pointers yet, so my question my be off or vague. Say you have an array:
Code:

` int arr[3][3]`
This array stores nine bytes of memory. When you pass this array to a function, does it only pass the start of the memory address, as opposed to the memory address of the whole nine bytes (start to end)?
If this is the case, it would make it more clear to me why you would need to specify the size of the second dimenstion of a 2 dimensional arrary.