"csx" <cs*@ms.com> wrote in message
news:bl**********@sparta.btinternet.com...
Hi everyone!
two quick questions relating to arrays.
Q1,
Is it possible to re-assign array elements?
Yes, individually.
int array[2][2] = {{2,4}, {4,5}};
array[1][1] = {2,3}
Invalid syntax.
Also, array[1][1] denotes a single type 'int' object.
How do you expect to store two values in a single object?
Guessing at which elements you really want to change:
array[1][0] = 2;
array[1][1] = 3;
just causes compiler errors! Do I have to assign at the beginning?
You must assign each element's value individually.
If your values form an appropriate 'pattern' this
can often be done in a loop.
Q2,
My other 'main' question relates to 2-Dimensional arrays.
I need to create the size of my 2D array at run time. I have a couple of
functions that provide the dimensions I need. I need to call the function
like:
two_dimensional_array(maxRows, numLeafs)
but then, assign the values after, hence my first question. Can values be
asigned after?
Yes.
Here is the code I currently have? Is this a good implementation? Any
suggestions would be much appreciated.
int ** myArray = 0; // in C++ 0==NULL
This is not an array, it's a pointer. It does
not point to any storage you can use.
You must allocate some memory and assign its
address to the pointer. I see you tried to
below, but fall short.
void two_dimensional_array(int rows, int cols)
That's not a very desriptive name. I'd call it
e.g. 'load2darray', 'populate2darray' or something like that.
Also note that you never call this function in the code
you've posted.
{
int Rows, Cols;
Array dimensions should be stored in type 'size_t'
Type 'int' won't necessarily be able to represent
all possible array index values. 'size_t' is
guaranteed to be able to.
if (myArray != NULL) {
for (int i = 0; i < Rows; i++) {
myArray[i] = new int[Cols];
}
}
}
int _tmain(int argc, _TCHAR* argv[])
int main(int argc, char *argv[]) {
No "Windows-isms" allowed here.
myArray = new *int[Rows];
Invalid syntax.
myArray = new int*[Rows];
But this only allocates storage for 'Rows' pointers.
There's still no storage for them to point to.
return 0;
}
The difficulty so many have with arrays is one reason
for the invention of standard library containers such
as std::vector.
I recommend you use containers instead of arrays in
C++ programs, unless there's a compelling reaons to
do otherwise (e.g. interface with C or existing,
nonmodifiable code).
std::vector<std::vector> > arr2d;
-Can do anything an array can do, and more.
-Does all the memory management for you.
-Is 'flexible' -- will grow or shrink its size as needed.
-Carries its size with it, no need to pass an extra 'size'
to functions as with arrays.
If you insist upon using arrays, perhaps you'll find
useful an example in C I posted to comp.lang.c today. Look
for my name as author of a post in a thread entitled
"Re: How to free allocated memory?"
It uses the C functions 'malloc()' and 'free()' for
memory management, but you should be able to drop in
new[] and delete[] in their place -- you *must* do this
if you're creating arrays of types which have ctors/dtors,
or they won't get called.
But if you don't have to, don't use arrays. Use containers.
Really.
HTH,
-Mike