char array in structs - why does strlen() return the correct value here?

后端 未结 3 1985
猫巷女王i
猫巷女王i 2020-12-03 17:38

I have a simple program like this:

#include 
#include 
#include 

typedef struct 
{
    int numberOfDays;
             


        
3条回答
  •  再見小時候
    2020-12-03 17:58

    The reason is that your months are indeed nul-terminated. If you have an array with 10 elements, and have an initialiser for 3 elements, then the rest is filled with 0's. If you had a month with 11 characters, the compiler would tell you. If you had a month with 10 characters, you would be in trouble because there would be no nul-termination, and the compiler wouldn't tell you.

提交回复
热议问题