Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Referencing a two dimentional array.

Status
Not open for further replies.

Dustman

Programmer
May 7, 2001
320
US
I've determined that knowing more than 5 languages has proven to be a neusance when trying to do simple things. Using C++ (on linux if it matters to ya)...

I've got a two dimentional array of my own Square class. The square class only contains int color and int area.

I've declaired it using
Square Sqr[r][c];

r and c are integers.

It is properly creating all of the classes (I can check it by outputing something when the Square constructor is called) but I can't seem to reference it.

1. How do I reference a single value? (ie: Sqr[2][3].color)

2. How do I make a pointer to this 2D array so that I can pass it to another procedure? -Dustin
Rom 8:28
 
try (Sqr[2][3]).color

a pointer to this array should be Square** sqrpointer; Greetz,
muppeteer.gif

NOSPAM_themuppeteer@hotmail.com (for mails, remove the NOSPAM_)

Don't eat yellow snow...and don't mess with your fstab!
 
Hmm.. (Sqr[2][3]).color still doesn't work. I keep gettin an error from the compiler. I'm not at that computer right now but I'll post the error when I get back to it.

Any other suggestions? -Dustin
Rom 8:28
 
Lets say we have object x.

class X has a function f().
and you have an 2d array Array.... as follows
Code:
X Array[10][10];

now we want to print the value returned by f() for each of the elements...
Code:
for (size_t i = 0; i < 10; i++)
   for (size_t j = 0; i < 10; i++)
      cout << (Array[i][j]).f() <<endl;

rember that comparison between a signed and unsigned int is indeed a warning, but not a error.

The pointer, isn't a pointer to a pointer, it is a single dimnention pointer... So in my example, you could write it as:
Code:
for (X* ptr = &(Array[0][0]);ptr <= &(Array[9][9]); ptr++)
    cout << ptr->f() <<endl;

Remember that Arrays in C++ are 0 based, and fixed size... If these restraints become a problem consider using a vector of vectors...
Code:
vector< vector<int> > Array(10);
//some code to populate the vectors would go here...  
for(vector< vector<int> >::iterator i = Array.begin(); 
    i != Array.end(); i++)
   for(vector<int>::iterator j = i->begin();
       j != i->end();  i++)
            cout << *j <<endl;
I used iterators in the above code because subscript operators would look exsactly like the array... The size of the vector is dynamic and adds the power (and burden) of iterators.

Happy hunting.
 
Okay, I think I get it now. I have to use a function to get my values. Referencing (Sqr[2][3]).color gives an error. Instead I made a function to return the color. (Sqr[2][3]).getColor() works like a charm.

My other problem was trying to make the array dynamic but still being able to reference it in all member functions of my class. To do this I'd have to use pointer notation as you stated above. To keep it simple I just hardcoded the size (luckily in my situation I know the max values). Thanks for the help! -Dustin
Rom 8:28
 
Dynamic arrays aren't really dynamic... You'll need to declare a pointer to a dynamically allocated array, or use the vector class... If you plan to do the dynamic sizing on your own, you have to remember that every time it needs to grow you have to allocate a new area of memory, copy everything over and then finish up by erasing the old memory.

If you have to write dynamic array, then you'll want to write it as follows (bare minimum):
When inserting
  • check if empty, if so, allocate space
  • check if full, is so, allocate space and copy data over to new memory, delete old space
  • put item into the array
When removing
  • check for empty, if so throw error or what have you
  • search for the item
  • if not found, do some kind of error code, or not
  • if found copy items after that location to the position before and decriment size (don't worry about the end, as it will be past the accessable size, until a new item is added at which point it is over written)
copy constructor
  • check the passed object for empty, if empty set your pointer null.
  • allocate a space = to the size (not neededly the cappacity as capasity is internal) of the passed
  • copy each element of the passed object into the one being created
Assigning
  • check for self assignment, if true return (it is already equal to it's self).
  • delete old memory
  • follow steps of Copy constructor
Destructing
[*] delete the memory, returning the resources to the heap.
[*] (defensive programing pratice is to)point all pointers to null AFTER DELETING (even though the pointer will just be destroyed as it goes out of scope)
[/list]

Of course the easy solution is to use an STL contianer like the vector.... That way you aren't reinventing the wheel.... you may want to read the man pages on the STL, the vector and the list.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top