i am working with 2 dimensional arrays... I fill a 2-dim array with numbers and have an int variable 'numRows'. For some reason when i re-assign the value of 'numRows' it also reassigns the value of 'array[0][0]'.....WHY??? PLEASE tell me where these 2 are connected or why this is happening... =(
when I call the print function the first row should be "0 1 2 3 4". I need it set up this way so that later i can make a select function that will create a new sub-table of different(smaller) size.
well since the array is part of a class it will be different sizes...it will always have 5 columns, but will have different numbers of rows.
my reference book says i dont need to declare the first parameter of the array...is this true? if not...how do i make a class of arrays that will be different sizes???
You don't need to define the first dimension of the array if you are initializing it at the time of definition. Like this:
int x[][2] = { {1, 1}, {2, 2} };
In that case, you still really know at compile time the first dimension.
If you want to create an array that is of varying size, unknown at compile time, you need to use a dynamically allocated array. This gets a little tricky.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
int** x; // an int pointer to an int pointer
int variableSize = 3; // this variable can change, it holds your first dimension
x = newint*[variableSize]; // creates this size 3 array of int pointers;
for (int i = 0; i < variableSize; ++i) // this loop iterates over the 3 elements of the int pointer array
x[i] = newint[5]; // and creates a new dimension of size 5 for each element
/* now you have an array x of dimensions [3, 5] you can fill it with int values as normal
when you are finished with this array or want to change the dimensions, you need to
free the memory! like this: */
for (int i = 0; i < variableSize; ++i)
delete[] x[i]; // use delete[] to free array of ints from each element of
// first dimension
delete[] x; // finally, free the array representing the first dimension itself
/* now you can use new on x as shown in the first part of the code to make new dimensions */
You need to be careful when you use new and delete. delete[] is what is required to free arrays from memory. It is different from delete without the brackets. Do not double-delete your variables, you you will have problems. Only delete things one time after newing them one time.
psault and mcleano are on the right track. I don't believe that you can declare an array in the class without a full declaration although I'm surprised that it compiles. If that class declaration compiles, I may have to research that further and figure out why.
Wow, this example compiles and runs but I don't know why. This seems like an interesting C++ trivia question. If it is simply undefined behavior, fine but why can't the compiler just complain about the struct declaration if it is wrong?
8 Non-static (9.4) members that are class objects shall be objects of previously defined classes. In particular,
a class cl shall not contain an object of class cl, but it can contain a pointer or reference to an object
of class cl. When an array is used as the type of a nonstatic member all dimensions shall be specified.