[SOLVED] C++ array | Sololearn: Learn to code for FREE!


[SOLVED] C++ array

How can I create an array in C++, if i have an int variable and I need that it become a length of my new array. int x[intvar] doesn't work.

12/7/2021 3:42:30 PM


9 Answers

New Answer


No buddy, I did understand your question. If you see example in the last slide, you will see that you can improvise from it, by changing the use of literal (20) by the use of a variable for defining number of array's element. int elements = 20; int *p = new int[ elements ]; // allocate memory // Do whatever you want here delete [] p; // free alocated memory


Look at the last slide here https://www.sololearn.com/learn/CPlusPlus/1632/?ref=app


I hope this link helps you. https://code.sololearn.com/cHwwXMcmj8c0


Ipang, maybe you don't understand me, I need to create an array, which length is stored in a variable


Ipang, sorry, i don't understand you. Thank you


Kind_Cat, Sorry too, but Idk how to explain it better than the tutorial.


RTB, please, read my question to the end. I wrote, that this doesn't work


Abhishek Pandey, i wrote that this does not work


Use this int var ; int arr[var] ;