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

发布于 2024-09-25 15:53:08 字数 242 浏览 4 评论 0原文

我正在用 gamestudio 进行一些实验。 我现在正在制作一款射击游戏。 我有一个数组,其中包含指向敌人的指针。当敌人被杀死时,我想将他从列表中删除。我也希望能够创造新的敌人。

Gamestudio 使用名为 lite-C 的脚本语言。它具有与 C 相同的语法,并且在网站上他们说它可以使用任何 C 编译器进行编译。它是纯 C 语言,没有 C++ 或其他任何东西。

我是 C 语言新手。我通常使用 .NET 语言和一些脚本语言进行编程。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

蓝眸 2024-10-02 15:53:08

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

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

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);
夕色琉璃 2024-10-02 15:53:08

C 中的数组一旦创建,就被设置。您需要一个动态数据结构,例如链接列表或数组列表

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

孤芳又自赏 2024-10-02 15:53:08

数组是静态的,因此您无法更改其大小。您需要创建链接列表数据结构。该列表可以根据需要增长和缩小。

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.

锦爱 2024-10-02 15:53:08

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

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

安稳善良 2024-10-02 15:53:08

正如 NickTFried 所建议的,链接列表是一种方法。
另一种方法是拥有一张足够大的桌子来容纳您将拥有的最大数量的物品并对其进行管理(哪些物品有效或无效,列表中当前有多少敌人)。

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

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

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

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.

画中仙 2024-10-02 15:53:08

我想降低数组大小,但效果不佳:

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

所以我尝试创建一个新数组,其大小基于保存的计数。

int count = 0;

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

int positives[count];

然后重新传递第一个数组中的元素以将它们添加到新数组中。

//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++; }
}

这里我们有一个全新的数组,其中包含我们想要的确切元素数量(在我的例子中)。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文