After surfing the google & MSDN for a few days I found an MSDN site
that explained how to "new" a multidimensional array in C++...but, I
don't know how to use it, for alas, my brain still doesn't understand
pointer very well. Here's the site: http://msdn.microsoft.com/library/de...w_operator.asp
And here's what I've come up with thus far to have a 2 dimensional
array of CStrings.
CString (*myTwoDimArray)[3000][MAX_PARAMS];
myTwoDimArray = new CString[0][3000][MAX_PARAMS];
(p.s. if you need to know what that first [] is on the new statement,
see the above referenced website...even though it barely explains it).
SO! Anyone wanna' tell me how to actually put a value in my 2D
CString array? Or how to delete it once I'm done?! 6 3338
TrustyTif wrote: After surfing the google & MSDN for a few days I found an MSDN site that explained how to "new" a multidimensional array in C++...but, I don't know how to use it, for alas, my brain still doesn't understand pointer very well. Here's the site:
http://msdn.microsoft.com/library/de...w_operator.asp And here's what I've come up with thus far to have a 2 dimensional array of CStrings.
CString (*myTwoDimArray)[3000][MAX_PARAMS]; myTwoDimArray = new CString[0][3000][MAX_PARAMS];
(p.s. if you need to know what that first [] is on the new statement, see the above referenced website...even though it barely explains it).
SO! Anyone wanna' tell me how to actually put a value in my 2D CString array? Or how to delete it once I'm done?!
#include <vector>
std::vector<std::vector<CString> > your2darray(width,
std::vector<CString>(height));
your2darray[x][y] = "asdf";
Memory management is done automatically.
- Pete
TrustyTif wrote: After surfing the google & MSDN for a few days I found an MSDN site that explained how to "new" a multidimensional array in C++...but, I don't know how to use it, for alas, my brain still doesn't understand pointer very well. Here's the site:
http://msdn.microsoft.com/library/de...w_operator.asp
And here's what I've come up with thus far to have a 2 dimensional array of CStrings.
CString (*myTwoDimArray)[3000][MAX_PARAMS]; myTwoDimArray = new CString[0][3000][MAX_PARAMS];
(p.s. if you need to know what that first [] is on the new statement, see the above referenced website...even though it barely explains it).
This doesn't make any sense. A multidimensional array with one or more a
zero-sized dimensions contains no elements. I don't see anything like
that on the linked web page.
If you really want to declare 'myTwoDimArray' as a _pointer_ to
two-dimensional array, you can allocate memory for it as follows
myTwoDimArray = new CString[1][3000][MAX_PARAMS];
// Note that 0 was replaced with 1
...
// Accessing elements
assert(3 < MAX_PARAMS);
(*myTwoDimArray)[10][3] = "test";
...
// Deallocating memory
delete[] myTwoDimArray;
But this is rather strange way to do this, to say the least.
You could also do this
myTwoDimArray =
(CString(*)[3000][MAX_PARAMS]) new CString[3000][MAX_PARAMS];
// Only two dimensions are really needed
...
// Accessing elements
assert(3 < MAX_PARAMS);
(*myTwoDimArray)[10][3] = "test";
...
// Deallocating memory
delete[] (CString(*)[MAX_PARAMS]) myTwoDimArray;
But this is also as ugly as it gets. And, strictly speaking, this code's
behavior is implementation defined since it relies on
'reinterpret_cast's. (I'll probably burn in hell for writing this one.)
If you really want to stick with built-in arrays, the proper way to do
it is to declare 'myTwoDimArray' as a pointer to a single-dimensional array
CString (*myTwoDimArray)[MAX_PARAMS];
myTwoDimArray = new CString[3000][MAX_PARAMS];
...
// Accessing elements
assert(3 < MAX_PARAMS);
myTwoDimArray[10][3] = "test";
...
// Deallocating memory
delete[] myTwoDimArray;
Looks much cleaner, doesn't it?
And, finally, you can make the whole thing to look even more clean if
you use 'std::vector's instead of built-in arrays (see Petec's response).
SO! Anyone wanna' tell me how to actually put a value in my 2D CString array? Or how to delete it once I'm done?!
See above.
--
Best regards,
Andrey Tarasevich
"TrustyTif" <ti***********@motorola.com> wrote in message
news:ab**************************@posting.google.c om... After surfing the google & MSDN for a few days I found an MSDN site that explained how to "new" a multidimensional array in C++...but, I don't know how to use it, for alas, my brain still doesn't understand pointer very well. Here's the site:
http://msdn.microsoft.com/library/de...w_operator.asp And here's what I've come up with thus far to have a 2 dimensional array of CStrings.
CString (*myTwoDimArray)[3000][MAX_PARAMS]; myTwoDimArray = new CString[0][3000][MAX_PARAMS];
(p.s. if you need to know what that first [] is on the new statement, see the above referenced website...even though it barely explains it).
SO! Anyone wanna' tell me how to actually put a value in my 2D CString array? Or how to delete it once I'm done?!
Actually you have a three dimensional array, count the pairs of square
brackets in the new statement.
You aren't going to be able to allocate a 2D array without understanding
pointers well. So I suggest you get a book and C or C++ and start reading.
On the other hand if you just want some code
CString (*myTwoDimArray)[MAX_PARAMS];
myTwoDimArray = new CString[3000][MAX_PARAMS];
myTwoDimArray[1][1] = "abc";
delete[] myTwoDimArray;
john
Andrey Tarasevich <an**************@hotmail.com> wrote in message news:<10*************@news.supernews.com>... TrustyTif wrote: After surfing the google & MSDN for a few days I found an MSDN site that explained how to "new" a multidimensional array in C++...but, I don't know how to use it, for alas, my brain still doesn't understand pointer very well. Here's the site:
http://msdn.microsoft.com/library/de...w_operator.asp
And here's what I've come up with thus far to have a 2 dimensional array of CStrings.
CString (*myTwoDimArray)[3000][MAX_PARAMS]; myTwoDimArray = new CString[0][3000][MAX_PARAMS];
(p.s. if you need to know what that first [] is on the new statement, see the above referenced website...even though it barely explains it).
This doesn't make any sense. A multidimensional array with one or more a zero-sized dimensions contains no elements. I don't see anything like that on the linked web page.
If you really want to declare 'myTwoDimArray' as a _pointer_ to two-dimensional array, you can allocate memory for it as follows
myTwoDimArray = new CString[1][3000][MAX_PARAMS]; // Note that 0 was replaced with 1 ... // Accessing elements assert(3 < MAX_PARAMS); (*myTwoDimArray)[10][3] = "test"; ... // Deallocating memory delete[] myTwoDimArray;
But this is rather strange way to do this, to say the least.
You could also do this
myTwoDimArray = (CString(*)[3000][MAX_PARAMS]) new CString[3000][MAX_PARAMS]; // Only two dimensions are really needed ... // Accessing elements assert(3 < MAX_PARAMS); (*myTwoDimArray)[10][3] = "test"; ... // Deallocating memory delete[] (CString(*)[MAX_PARAMS]) myTwoDimArray;
But this is also as ugly as it gets. And, strictly speaking, this code's behavior is implementation defined since it relies on 'reinterpret_cast's. (I'll probably burn in hell for writing this one.)
If you really want to stick with built-in arrays, the proper way to do it is to declare 'myTwoDimArray' as a pointer to a single-dimensional array
CString (*myTwoDimArray)[MAX_PARAMS]; myTwoDimArray = new CString[3000][MAX_PARAMS]; ... // Accessing elements assert(3 < MAX_PARAMS); myTwoDimArray[10][3] = "test"; ... // Deallocating memory delete[] myTwoDimArray;
Looks much cleaner, doesn't it?
And, finally, you can make the whole thing to look even more clean if you use 'std::vector's instead of built-in arrays (see Petec's response).
SO! Anyone wanna' tell me how to actually put a value in my 2D CString array? Or how to delete it once I'm done?!
See above.
Thanks Andrey! Both of your suggestions worked wonders!
TrustyTif posted: After surfing the google & MSDN for a few days I found an MSDN site that explained how to "new" a multidimensional array in C++...but, I don't know how to use it, for alas, my brain still doesn't understand pointer very well. Here's the site:
http://msdn.microsoft.com/library/de.../en-us/vclang9 8/html/_pluslang_new_operator.asp
And here's what I've come up with thus far to have a 2 dimensional array of CStrings.
CString (*myTwoDimArray)[3000][MAX_PARAMS]; myTwoDimArray = new CString[0][3000][MAX_PARAMS];
(p.s. if you need to know what that first [] is on the new statement, see the above referenced website...even though it barely explains it).
SO! Anyone wanna' tell me how to actually put a value in my 2D CString array? Or how to delete it once I'm done?!
Would any sort of variation of the following work?:
(int[x][y]) *p2DArray;
p2DArray = new int[x][y];
p2DArray[3][3] = 4;
Just a thought!
-JKop
"John Harrison" <jo*************@hotmail.com> wrote in message news:<2i************@uni-berlin.de>... "TrustyTif" <ti***********@motorola.com> wrote in message news:ab**************************@posting.google.c om... After surfing the google & MSDN for a few days I found an MSDN site that explained how to "new" a multidimensional array in C++...but, I don't know how to use it, for alas, my brain still doesn't understand pointer very well. Here's the site:
http://msdn.microsoft.com/library/de...w_operator.asp And here's what I've come up with thus far to have a 2 dimensional array of CStrings.
CString (*myTwoDimArray)[3000][MAX_PARAMS]; myTwoDimArray = new CString[0][3000][MAX_PARAMS];
(p.s. if you need to know what that first [] is on the new statement, see the above referenced website...even though it barely explains it).
SO! Anyone wanna' tell me how to actually put a value in my 2D CString array? Or how to delete it once I'm done?!
Actually you have a three dimensional array, count the pairs of square brackets in the new statement.
You aren't going to be able to allocate a 2D array without understanding pointers well. So I suggest you get a book and C or C++ and start reading.
On the other hand if you just want some code
CString (*myTwoDimArray)[MAX_PARAMS]; myTwoDimArray = new CString[3000][MAX_PARAMS];
myTwoDimArray[1][1] = "abc";
delete[] myTwoDimArray;
john
A good idea to create a 2d array would be to do as follows:
CString **myTwoDimArray;
myTwoDimArray = new CString*[3000]; //an array of pointers.
for(int i=0;i<3000;i++){
myTwoDimArray[i] = new CString[MAX_PARAMS]; //init each pointer.
}
To use elements in this array:
myTwoDimArray[0][0] = "Str1";
and so on..
To cleanly de-allocate:
for(int i=0;i<3000;i++){
delete []myTwoDimArray[i];
}
delete []myTwoDimArray;
Hope this helps.
-BG This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: * ProteanThread * |
last post by:
but depends upon the clique:
...
|
by: cody |
last post by:
What are multi file assemblies good for?
What are the advantages of using multiple assemblies (A.DLL+B.DLL) vs. a
single multi file assembly (A.DLL+A.NETMODULE)?
|
by: mimmo |
last post by:
Hi! I should convert the accented letters of a string in the correspondent
letters not accented. But when I compile with -Wall it give me:
warning: multi-character character constant
Do the...
|
by: Philip |
last post by:
Hi, first of all I'm new to this whole .net thing so please forgive my
ignorance. Now I have a C++ object with me, and I want to be able to
call its methods from VB.net.
e.g.
Foo::Foo
{
}...
|
by: bobwansink |
last post by:
Hi, I'm relatively new to programming and I would like to create a C++
multi user program. It's for a project for school. This means I will
have to write a paper about the theory too. Does anyone...
|
by: bonk |
last post by:
Hello I am acessing a Dictionary<TKey,TValuefrom multiple threads and
often in a foreach loop. While I am within one of the foreach loops the
other threads must not modify the collection itself...
|
by: ajitpsingh |
last post by:
Hi,
I recived Error message when acessing OLE Object.
"Cannot start the source application for this object"
in MS Excel.
|
by: Sabri.Pllana |
last post by:
We apologize if you receive multiple copies of this call for papers.
***********************************************************************
2008 International Workshop on Multi-Core Computing...
|
by: mknoll217 |
last post by:
I am recieving this error from my code:
The multi-part identifier "PAR.UniqueID" could not be bound.
The multi-part identifier "Salary.UniqueID" could not be bound.
The multi-part identifier...
|
by: swamimeenu |
last post by:
hi,
I am facing the problem in vb tht if more than one user accessing my program from various systems,if both of them acessing the progam at a same time (ie) for feeding data in it ,at tht time i...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
| |