Passing multi-dimensional array

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2007
Posts: 5
Reputation: delner is an unknown quantity at this point 
Solved Threads: 0
delner delner is offline Offline
Newbie Poster

Passing multi-dimensional array

 
0
  #1
Mar 27th, 2007
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...

  1. void printBoard(Cell board[][], int rows, int cols)
  2. {
  3. system("cls");
  4.  
  5. for(int i=0; i<rows; i++)
  6. {
  7. ...
  8. }

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?
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 19
Reputation: Masood Ali is an unknown quantity at this point 
Solved Threads: 1
Masood Ali's Avatar
Masood Ali Masood Ali is offline Offline
Newbie Poster

Re: Passing multi-dimensional array

 
0
  #2
Mar 27th, 2007
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).
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Passing multi-dimensional array

 
0
  #3
Mar 27th, 2007
> 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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC