but I have no idea how to use it as a 2d array, eg array[5][5]. Could anyone shed some light on this?
Thanks
"Say you're hanging from a huge cliff at the top of mt. everest and a guy comes along and says he'll save you, and proceeds to throw religious pamphlets at you while simultaniously giving a sermon." - Dustin G
"Say you're hanging from a huge cliff at the top of mt. everest and a guy comes along and says he'll save you, and proceeds to throw religious pamphlets at you while simultaniously giving a sermon." - Dustin G
IMO it's easier to do: (dont forget that sizeof)
int* array = (int*)malloc(width * height * sizeof(int));
and then access co-ordinates 7,3 by using the formula Erik gave:
int number = array[7 * width + 3];
You can make it easier by making functions or macros to do that sum for you.
Using pointers-to-pointers and loops to allocate lists of memory seems a bit of overcomplication. Freeing the array is complicated. You only need one Free with the formula. Also the list of pointers will use more memory.
Note: the formula for 3D arrays is (z * height * width) + (y * width) + x
Note 2: use C++ and do int* array = new int[width * height];
i guess there's no way of doing array[x][y] then? It's just for the sake of neatness really
"Say you're hanging from a huge cliff at the top of mt. everest and a guy comes along and says he'll save you, and proceeds to throw religious pamphlets at you while simultaniously giving a sermon." - Dustin G
You can reference it like that in FM's method but like Tigs says it makes it more complicated. Alternatively you could make a class and you may be able to use operator overloading to get [] to work how you want.
Operator Overloading is good but in this case it's a bit more trouble than it's worth if the designer is after "neatness" I assume your using C, btw? It's been a while since I last used it, but I dont think Operator Overloading is available in that language? Only C++? Perhaps I'm wrong, as I said it has been a while.
CBFM's method works the way you want, though you wouldnt want to be doing that many times, since you're increasing the array greatly with each loop it constantly has to find new space if the space it previously allocated becomes too small.
Microsoft Visual C++ and other good compilers MIGHT be able to minimize the code when it's compiled so that it isnt executing a loop but finds all the space straight away. But that depends on the compiler -- I certinaly dont know for sure which (if any) would be able to do this.
Mike
"Now I guess we're... 'Path-E-Tech Management'" -Dilbert
Aren't most C compilers also C++ compilers? Why does anyone still use C these days if C++ is around? Isn't C++ entirely superior? Classes are too cool not to use. Only disadvantage I can see to C++ is a few CPU cycles extra here and there, but with a modern machine doing 3 billion cycles per second, that's not too much of a worry.
My idea's good if you're going to change the number of rows, as all you are doing is creating a new array of pointers, copying the old array and deleting it. With one big chunk, the overhead is far greater.
Sorry, didnt read your post properly chris daft mistake
I'm using bits of C and C++, whichever's easier/most powerful in whatever situation
Edited by the Author.
"Say you're hanging from a huge cliff at the top of mt. everest and a guy comes along and says he'll save you, and proceeds to throw religious pamphlets at you while simultaniously giving a sermon." - Dustin G
Well I'd say C++ is easier and more powerful in all cases. Classes, inheritence, encapsulation, polymorphism, constructors, destructors, void pointers, overloading, operator overloading, exceptions, new, delete and STL all feature in C++ to name a few.
While FM's method may make it easier to add new rows, it also uses more memory. Personally, I'd use one of the STL classes like a vector or something, and have all the memory handled for you