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

前端 未结 6 494
死守一世寂寞
死守一世寂寞 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:39

    They are equivalent regarding the generated code (at least in optimised builds) because when an array is initialised with {0} syntax, all values that are not explicitly specified are implicitly initialised with 0, and the compiler will know enough to insert a call to memset.

    The only difference is thus stylistic. The choice will depend on the coding standard you use, or your personal preferences.

提交回复
热议问题