passing multidimensional arrays to functions

This is a discussion on passing multidimensional arrays to functions within the C Programming forums, part of the General Programming Boards category; i have a multidimensional array declared in main, called: grid[3][4]; and i need to pass it to a function get_data, ...

  1. #1
    Unregistered
    Guest

    Angry passing multidimensional arrays to functions

    i have a multidimensional array declared in main, called:

    grid[3][4];

    and i need to pass it to a function get_data, to fill the array, then i need to pass it to a function called display_data to display the data, i have all of the other code figured out but i just cant get the array to pass between functions.

  2. #2
    Unregistered
    Guest
    here is my code can you tell me what is wrong:

    void get_grid(grid_array[][4], int num_rows)
    void display_grid(grid_array[][4], int num_rows)

    void main(void)
    {
    int grid_array[3][4];
    get_grid(grid_array, 3);
    display_grid(grid_array, 3);
    }
    void get_grid(int grid_array[][4], int num_rows)
    {
    FILE *input;
    int row, column;

    input = fopen("index", "r");

    for (row = 0; row < 3; row++)
    {
    for(column = 0; column < 4; column++)
    fscanf(input, "%d", grid_array[row][column]);
    }
    }
    void display_grid(int grid_array[][4], int num_rows)
    {
    int i, j;

    for (i = 0; i < 3; i++)
    {
    for(j = 0; j < 4; j++)
    printf("%d", grid_array[i][j]);
    printf("\n");
    }
    }

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > here is my code can you tell me what is wrong:

    Yep:

    > void main(void)

    There ya go.

    Code:
    void get_grid(int grid_array[][4], int num_rows) 
    { 
        FILE *input; 
        int row, column; 
    
        input = fopen("index", "r"); 
    
        for (row = 0; row < 3; row++) 
        { 
            for(column = 0; column < 4; column++) 
                fscanf(input, "%d", grid_array[row][column]); 
        } 
    }
    There is no point in having 'num_rows' in this function if you aren't even using it. Replace the first loop with:

    for( row=0; row<num_rows; row++ )

    There's a start.

    The next problem is that you have to use the address of the cell, not just the cell. Keeping in mind that you cannot use fscanf() the same as fprintf(), one of the two must be wrong.

    Thus, if 'fprintf' works without you specifying the address of the variable, you know that fscanf requires it.

    Therefore, the following must be true:

    fscanf( input, "%d", &array[x][y] );

    Since:

    fprintf( output, "%d", array[x][y] )

    works.

    /** Edit: I realize that you're using printf, not fprint, but the same reasoning applies. One you provide the variable, the other, a pointer to the variable (print/scan respectivly). **/

    For future reference, you'll want to state what your program is doing or not doing, and what you expect it to do, rather than have us read lines and lines of random code to decypher what they do.

    Quzah.
    Last edited by quzah; 11-14-2001 at 04:16 PM.
    Hope is the first step on the road to disappointment.

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,853
    To pass a multi dimensional array (not of strings)

    Code:
    //prototype
    void get_grid(int **, int ); 
    //call
    get_grid(&grid_array, 3);
    //function
    void get_grid(int **grid_array, int num_rows) 
    { 
      FILE *input; 
      int row, column,*pGrid_Array=NULL; 
      input = fopen("index", "r"); 
      //lock the array to a pointer with a type cast back to the orig type
      pGrid_Array=(int*)grid_array;
      //ect
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  5. #5
    Registered User goran's Avatar
    Join Date
    Sep 2001
    Posts
    68
    here's a very simple way to pass a 2D array,

    in main, use

    display_grid(grid_array);

    and while defining;

    void display_grid(int new_array[3][4])
    {
    /* whatever you do to the array gets reflected in grid_array in main */
    return;
    }

    cheers,
    I don't wait for the future; it comes soon enough.

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: 12
    Last Post: 06-06-2008, 05:26 PM
  4. Help Understanding Passing Arrays To Functions
    By jrahhali in forum C++ Programming
    Replies: 7
    Last Post: 04-10-2004, 02:57 PM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21