-
pointers to 2d arrays
I have a class that works something like this:
Code:
class bleh {
public:
char *charArray;
bleh(int size);
private:
};
bleh::bleh(int size) {
charArray = new char[size];
}
then I realized that I wanted charArray to be 2d
however,
Code:
class bleh {
public:
char *charArray;
bleh(int size1, size2);
private:
};
bleh::bleh(int size1, int size2) {
charArray = new char[size1][size2];
}
doesn't werk
the only thing that I can get to work is somethint like:
Code:
class bleh {
public:
char charArray[100][100];
bleh(int size);
private:
};
bleh::bleh(int size) {
}
Keeps yelling about either (based on small changes on my part):
A) doesn't like pointer
B) can't cast
C) doesn't like that a 2d array have variables for it's size
Any clues how it can be done?
-
Mabye this might help some.
Code:
class bleh {
private: // might want it private
char charArray[100][100];
bleh(int size);
public: //Might want this public
};
bleh::bleh(int size) {
}
This might not help much but i changes private and public. Not sure if i can help you with the coding much. Ill try.
-
Or you can try something like this:
Code:
char** array;
const unsigned ROWS = 10;
const unsigned COLS = 10;
// allocate memory
array = new char*[ROWS];
for (i = 0; i < ROWS; ++i)
array[i] = new char[COLS];
...
// clean up memory
for (i = 0; i < ROWS; ++i)
delete array[i];
delete array;
-
thnx
ah people are up this late ^_^ good
Salem's answer is what I'm wanting, thnx
I did a search and didn't find that post =/
srry
-
You can use one dimentional array then deal with it as a two dimentional array.
int *array;
array = new int[columns*rows]
so it's like putting the rows next to each other.
You can access each element by the index array[x, y];
like this;
array[x, y]; is array[ columns * x + y ];
where columns is the number of columns in the array.
You can put it in a function it will make it easier for you.
The function will return a pointer.
Do you get it.