Thread: Passing 2D array to a function

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    14

    [Solved] Passing 2D array to a function

    Edit: Thanks for all the answers.

    Hi,

    How do I pass 2D array to a function correctly?

    Here's my code which doesn't work.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    #define MAX_ROW    2
    #define MAX_COLUMN 2
    
    void display_seats(char **);
    
    int main(void)
    {
        char seats[MAX_ROW][MAX_COLUMN] = { 0 };
    //    int input_row, input_column;
        int i, j;
        
        /* Init the row  and columns */
        for(i = 0; i < MAX_ROW; i++)
        {
            for(j = 0; j < MAX_COLUMN; j++)
            {                        
                seats[i][j] = 'X';
            }   
        }
    
        display_seats(seats);
    
        return EXIT_SUCCESS;
    }
    
    void display_seats(char **seats)
    {
        int i, j;
        
        for(i = 0; i < MAX_ROW; i++)
        {
            for(j = 0; j < MAX_COLUMN; j++)
            {                        
                printf("%c", seats[i][j]);
            }
            printf("\n");   
        }
    }
    MSVC++ 6 gives me two warnings,
    warning C4047: 'function' : 'char ** ' differs in levels of indirection from 'char [2][2]'
    warning C4024: 'display_seats' : different types for formal and actual parameter 1

    Thanks.
    Last edited by Krupux; 09-04-2003 at 06:55 PM.

  2. #2
    Wannabe Coding God
    Join Date
    Mar 2003
    Posts
    259
    &seats[0] - I think that should work
    They say that if you play a Windows Install CD backwords, you hear satanic messages. That's nothing; play it forward and it installs Windows.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    14
    Originally posted by Shogun
    &seats[0] - I think that should work
    Hi,

    If you mean
    Code:
    display_seats(&seats[0]);
    That doesn't work...

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    14
    Originally posted by Salem
    Definition and declaration should be
    Code:
    void display_seats(char seats[][MAX_COLUMN]);
    Hi,

    Yes that works. Thanks.

    Any reason why it has to be declared/used that way? and not

    Code:
    void display_seats(char **seats);

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >Any reason why it has to be declared/used that way?

    http://www.eskimo.com/~scs/C-faq/q6.18.html
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 10-21-2007, 07:44 AM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. passing 2D array to a function
    By ashesh in forum C Programming
    Replies: 4
    Last Post: 06-09-2003, 11:16 PM
  5. passing an array to a function (2d array)
    By revelation437 in forum C Programming
    Replies: 5
    Last Post: 03-11-2003, 03:32 PM