Solved

How do you pass a two-dimension INT array between functions?

Posted on 2004-10-26
220 Views
Last Modified: 2013-11-20
I have forgotten the simple C method for passing an array of INTs between functions - and I'm not near my library where I could look it up without shame.  So here we go:

I have a two-dimension array, playerBoard, that describes a 20x20 playing board:

      int playerBoard[20][20];

...and I need to send it to a function (actually, quite a few functions) to initialize it to -1's, and later to place playing pieces, display the results to an ASCII terminal, that sort of thing.  At present, the preferred method is to make the int array global, and let everybody/everything have a crack at it.  I'm looking for a more secure (data-wise) method to pass the information back and forth.  (Dopey me, *I* know that the data stays in the same place, that a pointer to the location is all that gets passed - but it's all lost in the antiquity -and chaos - of age...)

Does anybody have any suggestions?  (Just thought I'd ask...)
0
Question by:LongFist
    5 Comments
     
    LVL 3

    Expert Comment

    by:Indrawati
    Here's an example:

    void foo(int param[20][])
    {
    }

    int main()
    {
          int board[20][20];
          foo(board);
    }
    0
     
    LVL 3

    Expert Comment

    by:Indrawati
    Sorry, foo should be:

    void foo(int param[][20])
    {
    }
    0
     
    LVL 3

    Accepted Solution

    by:
    If you really want to make sure that board is declared as

    int board[20][20];

    you can do:

    void foo(int (&param)[20][20])
    {
    }

    Now if you declare board as anything other than int[20][20], compiler will complain.
    0
     
    LVL 1

    Author Comment

    by:LongFist
    So, if I read you right, the previous declaration:

        int foo (int param[][20]);

       ...would allow me to pass a two-dimensional int array of [n] first dimensions, as long as the second dimension was 20, right?  And I would still simply call it like:

       foo(board);

       ...to make it "do it's thing".  Right?  Or am I still confused?

    0
     
    LVL 1

    Author Comment

    by:LongFist
    This really rocks!  I took the definition a little farther by setting the board dimensions via [coinst int]s - that way, they always match, no matter how large (or small) the board needs to be!

    Thanks again!!!
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

    884 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now