c++ - strcmp
迷茫
迷茫 2017-04-17 15:03:10
0
2
654
char a[2] = { '1','2' };
printf("%d", strcmp(a, "12"));

结果为 1

一个没有结束符,一个有结束符,为什么没有的比有的大?

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(2)
小葫芦

Char is generally regarded as an unsigned integer, a[2] is a random value, and there are only two situations: greater than 0 and equal to 0.

Most of the time, a[2] is greater than '

小葫芦

Similar to the previous question - that question, my answer was trampled by the crowd - strings are strings, and character arrays are character arrays. The strcmp function requires you to pass in a string, because only strings have

char a[] = {'1', '2', 'rrreee'};
printf("%d", strcmp(a, "12"));
The result you get is not actually 1, but a random value. Because the strcmp function does not know the length of

, it performs an out-of-bounds access to memory. a

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template