7

如果我有int someArray[100]包含各种元素中的项目,我怎样才能将它们全部重置为 0?

基本问题,我知道,但我通常使用 Objective-C 数组,但为此我只是存储一些数字,并希望“老派”地处理它。

4

2 回答 2

15

You need to set each element to the value you want:

for (int i = 0; i < 100; ++i)
    SomeArray[i] = 0;

For the specific case of integers and the value 0, you can also make use of the fact that their bitwise representation is known (all bits are 0), and so you can use memset:

memset(SomeArray, 0, sizeof(SomeArray));
于 2012-06-05T13:43:30.953 回答
13

您可以使用memset

memset(someArray, 0, sizeof(someArray));
于 2012-06-05T13:43:09.180 回答