• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Two-dimensional array question C++

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

DrkLrdsSlv

Member
Joined
Mar 5, 2005
Location
Illinois
I'm currently in an intro C++ course in college and I'm working on an assignment in which I need to pass a two-dimensional array to a function. (The program is one a theoretical zookeeper would use to keep track of how much food monkeys are eating over the course of a week).

Now the book has me creating two global constants for the rows and columns:

Code:
#include <iostream>
using namespace std;

const int MONKEYS = 3;
const int DAYS = 7;

int main()
{
   // Function prototype
   void getFoodEaten(double [][DAYS]);

  // Declare array
   double food[MONKEYS][DAYS];

  // Get the food amount eaten
  getFoodEaten(food);
}

void getFoodEaten(double food[][DAYS])
{
   // Insert code here
}

I've left out bits of the code irrelevant to my question.

We've pretty much been taught to avoid global variables like the plague, but of course the book does it a different way. How would I have to alter the function prototype/header in order to allow me to move the constants to main?
 
Last edited:

cyberfish

Member
Joined
May 23, 2008
Location
London, England
IMHO, and I've seen it in many code written by professionals, global constants are ok. Global variables are a lot nastier because of access issues, recursion, and things like that.
 

Omsion

Member
Joined
Mar 6, 2006
Your course likely has not gotten there yet, but this is done with pointers. A brief overview of what they are and what they have to do with arrays:

An array in C/C++ is a contiguous block of memory, and a pointer p "points" to the start of that block of memory (aka p stores the memory address of the start). Iterating through the array is simply offsetting the memory address stored by the pointer by the size of the data field - so to get to index 4, the program would look in the address p + 4 x <datasize>.

So a 2D-array would be a double pointer, q. q contains the address of p, which contains the address of the start of a block of memory that is an array - the first row. Offsetting q will get you the address of other pointers to memory blocks that hold the other rows of the array.

No code right now, though I'm sure someone else will be along shortly. Hopefully that was clear - pointers generally cause headaches for people just learning them.
 
Last edited:

cyberfish

Member
Joined
May 23, 2008
Location
London, England
A 2D array is not a double pointer. It's a continuous block of memory just like a regular array, except the index is calculated using 2 indices and the width.

You can't do something like declaring 10 different arrays of 10 elements each, and assign them to a 10 elements array of pointers. You would actually need an array of pointers for that.

And passing 2D (or higher dimensional) arrays to functions are perfectly legal, as long as all dimensions (except the first one) are given.
 

Cowboy Shane

Member
Joined
Jan 9, 2002
Location
Augusta, GA
Setting up constants like that is, for small learning programs like this one, a good programming practice. You've avoided the use of magic numbers and you've made it easy to update the values in one place.

A better technique for shrink-wrap software would be to allow the user to easily update things like monkey counts.
 

Omsion

Member
Joined
Mar 6, 2006
A 2D array is not a double pointer. It's a continuous block of memory just like a regular array, except the index is calculated using 2 indices and the width.

You can't do something like declaring 10 different arrays of 10 elements each, and assign them to a 10 elements array of pointers. You would actually need an array of pointers for that.

And passing 2D (or higher dimensional) arrays to functions are perfectly legal, as long as all dimensions (except the first one) are given.
Well, if you need to have dynamic array sizes, doing the last isn't possible. But yes, I think I was trying to generalize too far.
 
OP
DrkLrdsSlv

DrkLrdsSlv

Member
Joined
Mar 5, 2005
Location
Illinois
cyberfish said:
IMHO, and I've seen it in many code written by professionals, global constants are ok. Global variables are a lot nastier because of access issues, recursion, and things like that.

I ended up sticking with the global constants. This is the "best practice", correct?

Your course likely has not gotten there yet, but this is done with pointers. A brief overview of what they are and what they have to do with arrays:

An array in C/C++ is a contiguous block of memory, and a pointer p "points" to the start of that block of memory (aka p stores the memory address of the start). Iterating through the array is simply offsetting the memory address stored by the pointer by the size of the data field - so to get to index 4, the program would look in the address p + 4 x <datasize>.

So a 2D-array would be a double pointer, q. q contains the address of p, which contains the address of the start of a block of memory that is an array - the first row. Offsetting q will get you the address of other pointers to memory blocks that hold the other rows of the array.

No code right now, though I'm sure someone else will be along shortly. Hopefully that was clear - pointers generally cause headaches for people just learning them.

I've read a bit on my own on pointers and have had trouble distinguishing them from reference variables, but I'll be learning about pointers in this week's lecture, which will hopefully clear that up.

Thanks for the advice guys!
 

cyberfish

Member
Joined
May 23, 2008
Location
London, England
I ended up sticking with the global constants. This is the "best practice", correct?
Certainly not the worst practice :). Don't worry about it. No one I know (profs and other programmers) mind global constants. Global variables are a different story, though (avoid them at all costs).

I've read a bit on my own on pointers and have had trouble distinguishing them from reference variables, but I'll be learning about pointers in this week's lecture, which will hopefully clear that up.
In terms of implementation, references and pointers are the same. If you write equivalent code using pointers and references, and look at the compiler assembly output, they will be very similar if not the same.

The difference is in syntax. References do not allow re-assignment, and do not require the asterisk for access.

Pointers are more generic and powerful, but references are more fool-proof. References are like aliases, which is one use of pointers.