Sizeof(char[]) in C

后端 未结 8 1791
情深已故
情深已故 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:33

    As Michael pointed out in the comments the strings are terminated by a zero. So in memory the first string will look like this

    "123\0"
    

    where \0 is a single char and has the ASCII value 0. Then the above string has size 4.

    If you had not this terminating character, how would one know, where the string (or char[] for that matter) ends? Well, indeed one other way is to store the length somewhere. Some languages do that. C doesn't.

提交回复
热议问题