You're trying to make a multidimensional array from a single pointer. I'm afraid you can't do that the way you're doing it. :/
I'd suggest you use an int**, and note that new[] should accept only one integer. Also you really should delete[] the memory you allocated with new[], for fear of a memory leak.
ADVANCE WARNING: You'll probably need a for loop (or something like a for loop) to initialize your 2d array. :)
Yes, I know what you meant. I just couldn't resist giving a counterexample. :)
I usually use the method you outlined as it allows BOTH dimensions to be determined dynamically.
The example I gave may miss the OP's needs as the 2nd dimension is fixed. Only the 1st dimension (the 3) can be set at run time with the method I showed.
@chipp:
If I just said "syntax error", that wouldn't tell you much about what's wrong, would it? :)
int *p[4];
Note in your example that the parenthesis (which are missing) are rather crucial.
can we just declare it like:
Nope. If you compiled it, you'd get an error along the lines of "invalid assignment". :/
what do you mean by these?
The first quote states that one of the dimensions (the 4) has to be fixed: it must be known at compile time and I daresay you'd have a lot of fun trying to change it at runtime. ;)
The second explains basically how the method works. p_fourInts here is a pointer to an array. Specifically, it's a pointer to an array that's four elements long, where the elements are integers. On the next line, we allocate three of those arrays for the pointer to point to.
after all, all i know this time, pointer is the type of "int *" but after this how could it called "pointer" because it separated by "(" like: int (*point) ? i'm still confused...
I can't think of a way in standard C++ off the top of my head without using inline assembly... there's a reason it's called fixed. *wink*
If you want control over the sizes of both dimensions at runtime, try something like this:
1 2 3 4
int** mat;
mat = newint*[size1]; //Create an array of pointers to ints.
for(int i = 0; i < size1; ++i)
mat[i] = newint[size2]; //Create an array of ints for each pointer in the above array.
...just remember to replace size1 and size2 with something meaningful and make sure that you delete [] each one of those arrays (including the array of pointers) after use...
1 2 3
for (int i = 0; i < size1; ++i)
delete [] mat[i]; //Delete each of the arrays of ints we allocated.
delete [] mat; //Delete the array of pointers.
Hopefully, my sleepiness didn't contribute to too many errors.
i wanna get it clear, so you called it fixed because it has to known before you declare the 2nd dimension in the next statement?
i see now, as far as i understand, you can explicitly declare the first dimension but it cannot be interrupted, otherwise (if it depends on user's input) you must use new, am i right?
CMIIW
oh i forgot it's night in there, while we're in asia is daylight :))
i wanna get it clear, so you called it fixed because it has to known before you declare the 2nd dimension in the next statement?
It's called fixed cause the dimension has to be known at compile time. Basically he created a 1 dimensional dynamic array of 1 dimensional fixed arrays, thus creating a 2 dimensional one.
i see now, as far as i understand, you can explicitly declare the first dimension but it cannot be interrupted, otherwise (if it depends on user's input) you must use new, am i right?
I don't understand that part... can you please rephrase that somehow?
what i mean about interruption is non-fixed value, that is it has to be a fixed value, like int (*p)[5] not like the "interruption" like: int (*p) [item1] which has non-fixed value.