Thread: Help Understanding Passing Arrays To Functions

  1. #1
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256

    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] )
    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?
    Clear the mines from our Shazbot!
    Get the enemy Shazbot!

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    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

  3. #3
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256
    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.
    Clear the mines from our Shazbot!
    Get the enemy Shazbot!

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    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))

  5. #5
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    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.

  6. #6
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256
    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?
    Clear the mines from our Shazbot!
    Get the enemy Shazbot!

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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] );
    My best code is written with the delete key.

  8. #8
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256
    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.
    Clear the mines from our Shazbot!
    Get the enemy Shazbot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Manipulating Character arrays in functions.
    By kbro3 in forum C++ Programming
    Replies: 11
    Last Post: 08-16-2008, 02:24 AM
  2. Replies: 2
    Last Post: 07-03-2008, 11:31 AM
  3. Replies: 7
    Last Post: 04-19-2006, 11:17 AM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  5. passing functions with variable
    By itld in forum C++ Programming
    Replies: 1
    Last Post: 10-30-2001, 11:43 PM