Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-10-26
5
Medium Priority
?
224 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
Comment
Question by:LongFist
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 3

Expert Comment

by:Indrawati
ID: 12418826
Here's an example:

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

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

Expert Comment

by:Indrawati
ID: 12418828
Sorry, foo should be:

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

Accepted Solution

by:
Indrawati earned 500 total points
ID: 12418836
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
ID: 12423395
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
ID: 12425240
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

636 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