How to initialize all members of an array to the same value?

后端 未结 23 1889
清歌不尽
清歌不尽 2020-11-21 04:34

I have a large array in C (not C++ if that makes a difference). I want to initialize all members of the same value.

I could swear I

23条回答
  •  心在旅途
    2020-11-21 05:05

    Back in the day (and I'm not saying it's a good idea), we'd set the first element and then:

    memcpy (&element [1], &element [0], sizeof (element)-sizeof (element [0]);

    Not even sure it would work any more (that would depend on the implementation of memcpy) but it works by repeatedly copying the initial element to the next - even works for arrays of structures.

提交回复
热议问题