Sizeof(char[]) in C

后端 未结 8 1786
情深已故
情深已故 2021-01-17 18:15

Consider this code:

 char name[]=\"123\";
 char name1[]=\"1234\";

And this result

The size of name (char[]):4
The size of n         


        
8条回答
  •  一生所求
    2021-01-17 18:38

    name = {'1','2','3','\0'};
    name1 = {'1','2','3','4','\0'};
    

    So

    sizeof(name) = 4;
    sizeof(name1) = 5;
    

    sizeof returns the size of the object and in this case the object is an array and it is defined that your array is 4 bytes long in first case and 5 bytes in second case.

提交回复
热议问题