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

前端 未结 4 1327
醉酒成梦
醉酒成梦 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 06:57

    You usually pass the length of the array along with the pointer. Nothing "knows" the length of anything.

    Memory is memory. An array of bytes could have 0x42 0x41 0x44 0x00 in it, which happens to be the string for "BAD" But it could just as well be an integer representing "1145127936" or a float representing "773.0625"

提交回复
热议问题