Help Understanding Passing Arrays To Functions

This is a discussion on Help Understanding Passing Arrays To Functions within the C++ Programming forums, part of the General Programming Boards category; When you pass an array to to a function, I read that you don't need to put the size of ...

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

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.

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.

"...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

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.

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:

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.

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.

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?

>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:

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.