| | |
Dynamic 2d array of CString
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2008
Posts: 31
Reputation:
Solved Threads: 1
Let me explain in more detail, I have:
int numData;
CStringArray MyArray[50];
I want to be able to set the value of any element like this:
MyArray[rownumber].SetAtGrow(colnumber, value);
Is there any way at the beginning I can set it to size numData like below?
CStringArray MyArray[numData]
//this gives me an error
int numData;
CStringArray MyArray[50];
I want to be able to set the value of any element like this:
MyArray[rownumber].SetAtGrow(colnumber, value);
Is there any way at the beginning I can set it to size numData like below?
CStringArray MyArray[numData]
//this gives me an error
C++ arrays are static in size, yes, but the Standard Template Library has containers that can do what you want. lists sounds like they may fit your needs, but vectors or deques may also be of use.
As far as this: You need to use dynamic memory to allocate an array to a size unknown at compile-time. Like this:
If this is all you need, you can ignore the stuff about the STL (standard template library). But if you want it to grow and shrink during runtime, you'll need a list or another container.
As far as this:
C++ Syntax (Toggle Plain Text)
CStringArray MyArray[numData]
C++ Syntax (Toggle Plain Text)
CStringArray* MyArray; MyArray = new CStringArray[numData]; //use the array delete[] MyArray;
Last edited by CoolGamer48; Jul 24th, 2008 at 5:03 pm.
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
•
•
Join Date: Jul 2005
Posts: 1,678
Reputation:
Solved Threads: 263
You can declare an array of type T using static memory like so:
T myArray[constInt];
where constInt resolves to an int that is declared const, meaning its value must be known at compile time and cannot be changed at run time. If you don't know what value you want/need for constInt at compile time, then you should declare the array on the heap, using dynamic rather than static memory. That is, you can do this:
int size;
cout << "enter value for size" << endl;
cin >> size;
T myArr * = new T[size];
Technically myArray and myArr aren't the same thing, but they behave the same and can be used interchangably, at least for most intents and purposes. If you don't want to monkey with memory handling yourself then using something like std::vector, which is a class that acts like a souped up array with [] operator and everything including memory handling.
CString is MSs string class that acts very much like std::string, if I'm not mistaken. Since the length of any given CString may vary I don't know whether you can do this:
CString myArray[10];
You can try it and see. Likewise you can try the dynamic declaration and see if that works, too. Otherwise, a vector of CStrings should work fine.
T myArray[constInt];
where constInt resolves to an int that is declared const, meaning its value must be known at compile time and cannot be changed at run time. If you don't know what value you want/need for constInt at compile time, then you should declare the array on the heap, using dynamic rather than static memory. That is, you can do this:
int size;
cout << "enter value for size" << endl;
cin >> size;
T myArr * = new T[size];
Technically myArray and myArr aren't the same thing, but they behave the same and can be used interchangably, at least for most intents and purposes. If you don't want to monkey with memory handling yourself then using something like std::vector, which is a class that acts like a souped up array with [] operator and everything including memory handling.
CString is MSs string class that acts very much like std::string, if I'm not mistaken. Since the length of any given CString may vary I don't know whether you can do this:
CString myArray[10];
You can try it and see. Likewise you can try the dynamic declaration and see if that works, too. Otherwise, a vector of CStrings should work fine.
Last edited by Lerner; Jul 24th, 2008 at 5:06 pm.
![]() |
Similar Threads
- Is there any more Documenation that I need? (C++)
- major help (C++)
- Array problems (C++)
- Declaration of dynamic pointer array puzzle. (C)
Other Threads in the C++ Forum
- Previous Thread: binay heap and binary search tree help
- Next Thread: error C2296
| Thread Tools | Search this Thread |
api array beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count data database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game getline google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text text-file tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






