If string arrays are null terminated in C, why are arrays of other data types not null terminated?

前端 未结 4 1326
醉酒成梦
醉酒成梦 2021-01-20 06:14

Strings, or arrays of characters in C must be null terminated to know where they end. Why does the same rule not apply to arrays of other types? eg. How does the computer kn

4条回答
  •  终归单人心
    2021-01-20 07:01

    A string in C is a sequence of char that is null terminated. It's a special case of a char array.

    You can have a char array that is not null terminated. For example:

    char x[] = { 'a', 'b', 'c' };
    

    Arrays in general are represented as a contiguous sequence of the base type in memory. The language itself doesn't keep track of how big an array is, you're expected to do that yourself.

提交回复
热议问题