initializing an array of ints

前端 未结 7 861
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-29 05:00

Does anyone have a way to initialize an array of ints (any multi-byte type is fine really), to a non-zero and non -1 value simply? By which I mean, is there a w

7条回答
  •  借酒劲吻你
    2020-11-29 05:49

    One line with pointers!

    for (int *p = a; p < (a + 30); p++) *p = 1;
    

    Or if you're prematurely afraid of performance hit caused by repeatedly calculating (a + 30):

    for (int *p = a + 30 - 1; p >= a; p--) *p = 1;
    

提交回复
热议问题