+ 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);
+ 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/
+ 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
+ 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 đ
+ 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.
- 5
Sorry