[SOLVED] C++ array | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

[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.

7th Dec 2021, 3:42 PM
Kind_Cat
Kind_Cat - avatar
9 Answers
+ 5
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
7th Dec 2021, 10:55 PM
Ipang
7th Dec 2021, 3:52 PM
Ipang
+ 2
I hope this link helps you. https://code.sololearn.com/cHwwXMcmj8c0
7th Dec 2021, 5:14 PM
SoloProg
SoloProg - avatar
+ 1
Ipang, maybe you don't understand me, I need to create an array, which length is stored in a variable
7th Dec 2021, 5:23 PM
Kind_Cat
Kind_Cat - avatar
+ 1
Ipang, sorry, i don't understand you. Thank you
8th Dec 2021, 4:06 PM
Kind_Cat
Kind_Cat - avatar
+ 1
Kind_Cat, Sorry too, but Idk how to explain it better than the tutorial.
8th Dec 2021, 6:09 PM
Ipang
+ 1
RTB, please, read my question to the end. I wrote, that this doesn't work
8th Dec 2021, 6:27 PM
Kind_Cat
Kind_Cat - avatar
0
Abhishek Pandey, i wrote that this does not work
8th Dec 2021, 4:05 PM
Kind_Cat
Kind_Cat - avatar
- 1
Use this int var ; cin>>var; int arr[var] ;
8th Dec 2021, 9:48 AM
Abhishek Pandey
Abhishek Pandey - avatar