nul terminating a int array

后端 未结 4 1979
青春惊慌失措
青春惊慌失措 2020-12-11 08:16

gcc 4.4.4 c89

I was just experimenting with a int array. And something just came to my mind. Can I nul terminate it. For example, I am using a 0 to nul terminate. Ho

4条回答
  •  一生所求
    2020-12-11 08:30

    In short, no. Technically nul characters are equally valid in strings too, it's just a convention that we use them for marking the end of a string, and all the standard library functions expect that. There are "double nul-terminated" strings that end in \0\0 for cases where a string needs to contain a \0, but then of course you have the problem of not being able to store \0\0 in the string.

    If you don't want to store an array's size separately (or use trickery like sizeof), you need to come up with a sentinel that can be stored in that type but you know won't be part of the array; you could use 45 as long as you're sure arr won't have that as a valid value, it just needs to be unique

提交回复
热议问题