Null byte and arrays in C

前端 未结 3 1469
滥情空心
滥情空心 2021-02-13 18:43

If I declare a char array of say 10 chars like so...

char letters[10];

am I creating a set of memory locations that are represented as chars fr

3条回答
  •  孤城傲影
    2021-02-13 19:21

    And throughout your programming in [Turbo(C++), try to use F7, or F8 and Alt+F4, you can see what's happening inside that will be much useful for a beginner who having doubts like this

    When ever you declaring a variable a seperate memory location will be alloted to that variable. In case of array variable like

    char letters[10];
    

    Ten memory space will get alloted to letters variable.

    And the size of memory allocation will get vary for different datatype(i.e. int,char,float...).

    Again in your case: if your want to store a name like "csstudent" in array you have declare an array size of "ten" even "csstudent" size is "nine", because the last index is to store "\0" character indicates the end of the string

    //1000,1001 are memory space alloted,may will vary in you system

       1000  1001  1002  1003  1004  1005  1006  1007  1008  1009  
     +-----+-----+-----+-----+-----+-----+-----+-----+-----+------+
     |     |     |     |     |     |     |     |     |     |      |
     | 'c' | 's' | 's' | 't' | 'u' | 'd' | 'e' | 'n' | 't' | '\0' |
     |     |     |     |     |     |     |     |     |     |      |
     +-----+-----+-----+-----+-----+-----+-----+-----+-----+------+
    

提交回复
热议问题