nul terminating a int array

后端 未结 4 1982
青春惊慌失措
青春惊慌失措 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

    Simply put, \0 is the same as 0, so if you have a zero in your array your code will mistake that for the array's end:

    int arr[] = {30, 450, 14, 5, 0, 10, '\0'};
                                 ^ this is the end of the array
    

    Edit: note that by "end of the array" I mean the conceptual end of the array as defined by your algorithm, rather than the actual extents of the data laid out in memory by the C programming language.

提交回复
热议问题