Thread: Passing a row of a Multidemensional Array to a function

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    28

    Passing a row of a Multidemensional Array to a function

    Let's say I have 2D array of N rows and M columns, how do pass one of the rows of this array to a function and the function can use this row as a 1D array? Thanks.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void foo( data_type array[SIZE] )
    {
    
    }
    
    ...
    
    data_type array[ROWS][SIZE];
    
    foo( array[x] );
    Enjoy.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    28
    >>>void foo( data_type array[SIZE] )
    when defining this function, do I must specify the array size as SIZE (here it corresponds to number of columns) ?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Passing a row to it is actually passing a pointer to the first element in whatever row. The size specifier was really just for illustration. Consider the following:
    Code:
    #include <stdio.h>
    #define SIZE 10
    
    void foo( char array[] )
    {
        printf("array is: %s\n", array );
    }
    
    int main( void )
    {
        char mdarray[][SIZE] =
        {
            "foo",
            "bar",
            "barfoo",
            "fweeeeee",
            "the end",
            0
        };
        int x;
    
        for( x = 0; mdarray[x][0]; x++ )
            foo( array[x] );
    
        return 0;
    }
    This should run through the list and stop when it hits the null (the zero).

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User Dave++'s Avatar
    Join Date
    Jun 2007
    Location
    Where the Buffalo Roam
    Posts
    40

    confirm multi to one row code?

    Way back when...quzah wrote:
    Code:
        for( x = 0; mdarray[x][0]; x++ )
            foo( array[x] );
    
        return 0;
    was array[x] supposed to be mdarray[x]?

    Regards,
    Dave
    "The thousand natural bugs that code is heir to" Codespeare

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yes. Way to necro a six year old thread!


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Passing a pointer to two-dimension array in a function
    By E_I_S in forum C++ Programming
    Replies: 11
    Last Post: 06-19-2008, 09:57 AM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  5. Passing my array to function
    By pooty tang in forum C Programming
    Replies: 8
    Last Post: 09-15-2004, 12:19 PM