johnbakers
johnbakers

Reputation: 24750

How to reset C array so all elements are 0

If I have int someArray[100] that contains items in various elements, how can I reset them all back to 0?

Basic question, I know, but I usually use Objective-C arrays however for this purpose I'm just storing some numbers and want to deal with it "old school."

Upvotes: 8

Views: 38608

Answers (2)

user25148
user25148

Reputation:

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

Upvotes: 15

user405725
user405725

Reputation:

You can use memset:

memset(someArray, 0, sizeof(someArray));

Upvotes: 13

Related Questions