954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Passing multi-dimensional array

So I'm trying to write a program to simulate Conway's game of life. I thought it'd be cool to declare a class called Cell and use an array of objects instead of an array of ints or bools. My problem is that when I go to compile a function to which I pass a multi-dimensional array of Cell, the compiler yells at me for not giving it a set size. (The size of my board varies). My code...

void printBoard(Cell board[][], int rows, int cols)
{
    system("cls");
 
    for(int i=0; i<rows; i++)
    {
    ...
}


With a little tinkering and browsing of samples on the internet, I noticed that making it a single dimensional array of ambiguous size works just fine, but soon as its more than one dimension, it says I must declare bounds.

I suspect there may be a way to work around this with pointers, but haven't figured it out yet. Suggestions?

delner
Newbie Poster
5 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

You have to specify at least first dimension otherwise its not going to work anyway.
The secong dimension can be left unspecified.
Try managing by specifying one dimension at least(its compulsory).

Masood Ali
Newbie Poster
19 posts since Mar 2007
Reputation Points: 10
Solved Threads: 1
 

> You have to specify at least first dimension otherwise its not going to work anyway.
> The secong dimension can be left unspecified.
Wrong way round - it's only the left-most dimension which can be left empty, all the minor dimensions need to be specified.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You