Sololearn: Learn to Code
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4
// Like "ENEMY enemies[100]", but from the heap ENEMY* enemies = malloc(100 * sizeof(ENEMY)); if (!enemies) { error handling } // You can index pointers just like arrays. enemies[0] = CreateEnemy(); // Make the array bigger ENEMY* more_enemies = realloc(enemies, 200 * sizeof(ENEMY)); if (!more_enemies) { error handling } enemies = more_enemies; // Clean up when you're done. free(enemies);
23rd May 2022, 4:34 PM
Ulysses Apokin
Ulysses Apokin - avatar
+ 3
What you're looking for is a dynamic array, whose equivalent in the C++ STL is std::vector https://m.cplusplus.com/reference/vector/vector/
23rd May 2022, 6:14 PM
XXX
XXX - avatar
+ 3
Manav Roy Then I think you should not get into problems like these for now. It's not like it's very complicated, in fact, a dynamic array is a very simple structure, but I think you should know the language you're using first. I say this because there isn't a specific way of making a dynamic array in C++ due to there being no standard way to reallocate memory. So you have to reply on C functions which are generally less preferred to the 'new' and 'delete' operators. Here is a solution to your problem if you're still interested, but std::vector should be the preferred way of doing this https://code.sololearn.com/c112t9UIu30K/?ref=app
24th May 2022, 6:26 AM
XXX
XXX - avatar
+ 2
this is dynamic memory allocation.. int size; cin>>size; int* arr= new int[size]; for(int i=0;i<size;i++) cin>>arr[i] Hope it will be helpful for you 😇
24th May 2022, 7:09 PM
Muhammad Yasin
Muhammad Yasin - avatar
+ 1
You can create a do-while loop. Place a counter in this loop. Make the numeric value of the counter an argument to the realloc function.
23rd May 2022, 5:08 PM
Ulysses Apokin
Ulysses Apokin - avatar
- 5
Sorry
25th May 2022, 9:57 AM
Lamih V.p
24th May 2022, 9:16 PM
Example Pro
Example Pro - avatar