Array initialization with {0}, {0,}?

前端 未结 6 495
死守一世寂寞
死守一世寂寞 2020-12-24 06:00

Say I want to initialize myArray

char myArray[MAX] = {0};  
char myArray[MAX] = {0,};  
char myArray[MAX]; memset(myArray, 0, MAX);  

6条回答
  •  失恋的感觉
    2020-12-24 06:36

    Assuming that you always want to initialize with 0.

    --> Your first way and 2nd way are same. I prefer 1st.

    --> Third way of memset() should be used when you want to assign 0s other than initialization.

    --> If this array is expected to initialized only once, then you can put static keyword ahead of it, so that compiler will do the job for you (no runtime overhead)

提交回复
热议问题