Thread: Passing a int[ ][ ] to a function

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    117

    Passing a int[ ][ ] to a function

    I looked online and did what they said, but something isn't right. I put
    "void seatChart (int AvailableSeats[][]);" which won't compile. I even put [14][19] in the braces which would compile then but quit on me as soon as it reaches a reference to AvailableSeats[x][y].

    What am I doing wrong? Thanks yet again!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void seatChart (int AvailableSeats[][]);
    
    int main()
    {
        float sale;
    
        int choice;
    
        int row;
        int seat;
    
        int AvailableSeats[14][19] = {0}; //seats[row][seat]
    
    
    
    
        for(;;)
        {
            seatChart(AvailableSeats[14][19]);
    
            printf("\n\nMenu:");
            printf("\n1) Buy ticket");
            printf("\n2)Total sell and exit");
            printf("\n\nEnter your choice : ");
            scanf("%d", &choice);
    
            if (choice == 1)
            {
                printf("\n\nEnter row: ");
                scanf("%d", &row);
    
                printf("\nEnter seat: ");
                scanf("%d", &seat);
    
                AvailableSeats[row][seat] = 1;
    
                continue;
            }
    
            if(choice == 2)
            {
                printf("\n\nTOTAL TICKETS SOLD: X");
                printf("\nTOTAL REVENUE: Y");
                break;
            }
    
            printf("Invalid choice");
    
        }
    
    
        return 0;
    }
    
    void seatChart (int AvailableSeats[][])
    {
        int i;
        int o;
    
        printf("\n* Seats available");
        printf("\n# Reserved Seats");
        printf("\nSeats:  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19");
    
    
    
        for(o = 0; o<=14; o++)
        {
            printf("\nRow %2d", o);
    
            for(i = 0; i<=19; i++)
            {
                if(AvailableSeats[o][i]==0)
                {
                    printf("  *");
                }
                if(AvailableSeats[o][i]==1)
                {
                    printf("  #");
                }
            }
        }
    
    }
    My Ctrl+S addiction gets in the way when using Code Blocks...

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    At a minimum, you must specify all but the leftmost dimension, and you can specify the leftmost dimension also.
    Code:
    void seatChart (int AvailableSeats[][19])
    Usually you would #define such constants so they can be easily changed and are better documented.

    Also, in your loop you are going up to <= the dimensions, but it should just be <, otherwise you go one-to-far.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Also, when calling it, use
    Code:
       seatChart(AvailableSeats);
    not
    Code:
       seatChart(AvailableSeats[14][19]);
    The first passes an array (which is what the function expects). The second form passes a single integer value (which is not what the function is written to expect).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    117
    ah, stupid of me, thanks!

    Edit: Thanks both of you! Two problems solved and program is working as expected Much appreciated!
    Last edited by JonathanS; 01-24-2012 at 12:53 AM.
    My Ctrl+S addiction gets in the way when using Code Blocks...

  5. #5
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Code:
    void seatChart(int *AvailableSeats, int h, int w);
    
    int main(void)
    {
            const int h = 14;
            const int w = 18;
            int AvailableSeats[h][w] = {0};
            /*...*/
            while(1) {
                    seatChart(AvailableSeats, h, w);
                    /*...*/
            }
            return 0;
    }
    
    void seatChart(int *AvailableSeats, int h, int w)
    {
            /*...*/
            int i, t;
            for (i = 0; i < h; ++i) {
                    /*...*/
                    for (t = 0; t < w; ++t) {
                            /*...*/
                    }
            }
    }
    Don't overthink this. `AvailableSeats` is just a contiguous block of memory with a size `sizeof(int) * h * w`. Pass a pointer to the start of the block and its dimensions. Then use these dimensions as limits for indexing into it.

    `for(; ;)` is old-school and unnecessary. I hear is ye olde days compilers could optimize it better or something, but that's on longer true and the current idiom for infinite loop that will be manually broken out of is `while (1)`.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by msh
    `AvailableSeats` is just a contiguous block of memory with a size `sizeof(int) * h * w`. Pass a pointer to the start of the block and its dimensions. Then use these dimensions as limits for indexing into it.
    If you want to use msh's suggestion, then the correct way to call the function is:
    Code:
    seatChart(&AvailableSeats[0][0], h, w);
    Quote Originally Posted by msh
    `for(; ;)` is old-school and unnecessary. I hear is ye olde days compilers could optimize it better or something, but that's on longer true and the current idiom for infinite loop that will be manually broken out of is `while (1)`.
    I think for(;;) is canonical and fine.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by laserlight View Post
    If you want to use msh's suggestion, then the correct way to call the function is:
    Code:
    seatChart(&AvailableSeats[0][0], h, w);
    Thanks for the catch. :)

    Quote Originally Posted by laserlight View Post
    I think for(;;) is canonical and fine.
    I find it ugly and obscure.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    If you are seeking an infinite loop, whatever works and is understandable is fine. Any preferences are stylistic, not technical.

    Given that the OP is using a break statement to end the "infinite" loop, I would generally prefer to do something like "int continuing = 1; while (continuing) .... " and set continuing to zero when done - rather than using break or continue statements.

    Incidentally, it still amuses me that folks in the "goto is evil" religion often make use of break or continue statements to break out or skip parts of infinite loops.... effectively, that is using them as closet goto statements.

    If it walks like a duck and quacks like a duck, then it is a good bet it is actually a duck.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    161
    Quote Originally Posted by laserlight View Post
    If you want to use msh's suggestion, then the correct way to call the function is:
    Code:
    seatChart(&AvailableSeats[0][0], h, w);

    I thought it was:
    Code:
    seatChart(h,w,AvailableSeats)
    
    void ( int h, int w, int seatChart[h][w]){...}   //VLA

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with passing a pointer to a function to another function
    By csepraveenkumar in forum C Programming
    Replies: 5
    Last Post: 03-20-2011, 01:13 PM
  2. passing to function
    By Suchy in forum C Programming
    Replies: 4
    Last Post: 02-19-2008, 02:00 AM
  3. Replies: 1
    Last Post: 09-04-2007, 10:00 PM
  4. 2D array passing to function
    By mkorolen in forum C Programming
    Replies: 5
    Last Post: 03-31-2002, 08:37 PM
  5. Passing values from function to function
    By itld in forum C++ Programming
    Replies: 2
    Last Post: 10-24-2001, 07:28 AM