我在比较两个相同的 char 字符串时遇到问题:
char string[50];
strncpy(string, "StringToCompare", 49);
if( !strcmp("StringToCompare", string) )
//do stuff
else
//the code runs into here even tho both strings are the same...this is what the problem is.
如果我使用:
strcpy(string, "StringToCompare");
代替:
strncpy(string, "StringToCompare", 49);
它解决了这个问题,但我宁愿插入字符串的长度而不是它本身。
这里出了什么问题?我该如何解决这个问题?