How to Initialize an array of user selected size
C / C++ / MFC
2
Posts
2
Posters
0
Views
1
Watching
-
Hi, How do I initilaize an array whose size will be determined by an user input(For example from a edit box ? I tried this but they didnt work... numsamples = m_numsamp; const long arr = numsamples; ULONG x[arr]; ULONG y[arr]; They returned error 2057 and 2466. Thanks! Deepak Samuel.
-
Hi, How do I initilaize an array whose size will be determined by an user input(For example from a edit box ? I tried this but they didnt work... numsamples = m_numsamp; const long arr = numsamples; ULONG x[arr]; ULONG y[arr]; They returned error 2057 and 2466. Thanks! Deepak Samuel.
You need to dynamically allocate your array. This is where pointers comes very handy!
ULONG* arrA = NULL; int nSize; // Get size of array from user .. // allocate array to given size arrA = new ULONG[nSize]; if(NULL != arrA) { // work with array. arrA[0] = 1234; cout << _T("yeah baby!") << arrA[0] << endl; // don't forget to clean up when finished delete [] arrA; {
I Dream of Absolute Zero