How can I change the size of an array in C?

Viewed 98125

I am experimenting a little bit with gamestudio. I am now making a shooter game. I have an array with the pointers to the enemies. When an enemy is killed, I want to remove him from the list. And I also want to be able to create new enemies.

Gamestudio uses a scripting language named lite-C. It has the same syntax as C and on the website they say, that it can be compiled with any C compiler. It is pure C, no C++ or anything else.

I am new to C. I normally program in .NET languages and some scripting languages.

6 Answers

I wanted to lower the array size, but didn't worked like:

myArray = new int[10];//Or so.

So I've tried creating a new one, with the size based on a saved count.

int count = 0;

for (int i = 0; i < sizeof(array1)/sizeof(array1[0]); i++){
  if (array1[i] > 0) count++;
}

int positives[count];

And then re-pass the elements in the first array to add them in the new one.

//Create the new array element reference.

int x = 0;

for (int i = 0; i < sizeof(array1)/sizeof(array1[0]); i++){
  if (array1[i] > 0){ positives[x] = array1[i]; x++; }
}

And here we have a brand new array with the exact number of elements that we want (in my case).

Related