如何更改C中的数组的大小?

时间:2023-01-01 21:27:49

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

我正在用gamestudio做一些实验。我现在正在做射击游戏。我有一个指向敌人的指针的数组。我想要的。当敌人被杀时。把他从名单上删除。我也想创造新的敌人。

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.

Gamestudio使用一种名为lite-C的脚本语言。它的语法和C一样,在网站上他们说,它可以用任何C编译器编译。它是纯C,没有c++或其他东西。

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

我是c的新手,我通常用。net语言和一些脚本语言编程,

5 个解决方案

#1


0  

Once an array in C has been created, it is set. You need a dynamic data structure like a Linked List or an ArrayList

一旦在C中创建了一个数组,它就被设置了。你需要一个动态的数据结构,比如一个链表或者一个数组列表。

#2


19  

You can't. This is normally done with dynamic memory allocation.

你不能。这通常通过动态内存分配来完成。

// 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


3  

Arrays are static so you won't be able to change it's size.You'll need to create the linked list data structure. The list can grow and shrink on demand.

数组是静态的,所以你不能改变它的大小。您需要创建链表数据结构。这个名单可以根据需求增长和收缩。

#4


0  

Take a look at realloc which will allow you to resize the memory pointed to by a given pointer (which, in C, arrays are pointers).

看看realloc,它将允许您调整给定指针指向的内存大小(在C中,数组是指针)。

#5


0  

As NickTFried suggested, Linked List is one way to go. Another one is to have a table big enough to hold the maximum number of items you'll ever have and manage that (which ones are valid or not, how many enemies currently in the list).

正如NickTFried所说,链表是一种方法。另一种方法是拥有一个足够大的表来容纳您所拥有的最大数量的项目并进行管理(哪些是有效的,哪些是无效的,列表中有多少敌人)。

As far as resizing, you'd have to use a pointer instead of a table and you could reallocate, copy over and so on... definitely not something you want to do in a game.

至于调整大小,你需要使用指针而不是表格,你可以重新分配,复制,等等。绝对不是你想在游戏中做的事情。

If performance is an issue (and I am guessing it is), the table properly allocated is probably what I would use.

如果性能是一个问题(我猜是),那么正确分配的表可能就是我要使用的。

#1


0  

Once an array in C has been created, it is set. You need a dynamic data structure like a Linked List or an ArrayList

一旦在C中创建了一个数组,它就被设置了。你需要一个动态的数据结构,比如一个链表或者一个数组列表。

#2


19  

You can't. This is normally done with dynamic memory allocation.

你不能。这通常通过动态内存分配来完成。

// 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


3  

Arrays are static so you won't be able to change it's size.You'll need to create the linked list data structure. The list can grow and shrink on demand.

数组是静态的,所以你不能改变它的大小。您需要创建链表数据结构。这个名单可以根据需求增长和收缩。

#4


0  

Take a look at realloc which will allow you to resize the memory pointed to by a given pointer (which, in C, arrays are pointers).

看看realloc,它将允许您调整给定指针指向的内存大小(在C中,数组是指针)。

#5


0  

As NickTFried suggested, Linked List is one way to go. Another one is to have a table big enough to hold the maximum number of items you'll ever have and manage that (which ones are valid or not, how many enemies currently in the list).

正如NickTFried所说,链表是一种方法。另一种方法是拥有一个足够大的表来容纳您所拥有的最大数量的项目并进行管理(哪些是有效的,哪些是无效的,列表中有多少敌人)。

As far as resizing, you'd have to use a pointer instead of a table and you could reallocate, copy over and so on... definitely not something you want to do in a game.

至于调整大小,你需要使用指针而不是表格,你可以重新分配,复制,等等。绝对不是你想在游戏中做的事情。

If performance is an issue (and I am guessing it is), the table properly allocated is probably what I would use.

如果性能是一个问题(我猜是),那么正确分配的表可能就是我要使用的。