由于某些原因,strcmp()没有返回0。这是代码:
#include <iostream>
#include <ccstring>
int main()
{
char buffer[2];
buffer[0] = 'o';
char buffer2[2];
char buffer2[0] = 'o';
cout<<strcmp(buffer, buffer2);
}
谢谢!
比较之前,先终止字符串。
#include <iostream>
#include <ccstring>
int main()
{
char buffer[2];
buffer[0] = 'o';
buffer[1] = 0; <--
char buffer2[2];
buffer2[0] = 'o';
buffer2[1] = 0; <--
cout<<strcmp(buffer, buffer2);
}
编辑:(2014年3月7日):
附加字符串初始化:
int main()
{
//---using string literals.
char* str1 = "Hello one"; //<--this is already NULL terminated
char str2[] = "Hello two"; //<--also already NULL terminated.
//---element wise initializatin
char str3[] = {'H','e','l','l','o'}; //<--this is not NULL terminated
char str4[] = {'W','o','r','l','d', 0}; //<--Manual NULL termination
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句