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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3827892/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:33:04  来源:igfitidea点击:

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

carraysresizesizearraylist

提问by Aaron de Windt

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 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 语言和一些脚本语言编程,

采纳答案by Aaron de Windt

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 中创建了一个数组,它就会被设置。您需要一个动态数据结构,如 Linked List 或 ArrayList

回答by dan04

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

回答by Aaron de Windt

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.

数组是静态的,所以你不能改变它的大小。你需要创建链表数据结构。该列表可以根据需要增长和缩小。

回答by dj2

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

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

回答by Matthieu

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.

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

回答by Ev0lv3zz

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

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