Initialization of a normal array with one default

2018-12-31 16:24发布

This question already has an answer here:

C++ Notes: Array Initialization has a nice list over initialization of arrays. I have a

int array[100] = {-1};

expecting it to be full with -1's but its not, only first value is and the rest are 0's mixed with random values.

The code

int array[100] = {0};

works just fine and sets each element to 0.

What am I missing here.. Can't one initialize it if the value isn't zero ?

2: Is the default initialization (as above ) faster than the usual loop through the whole array and assign a value or does it do the same thing?

13条回答
浅入江南
2楼-- · 2018-12-31 17:01

For the case of an array of single-byte elements, you can use memset to set all elements to the same value.

There's an example here.

查看更多
登录 后发表回答