将字符比较在一起



我对C++还很陌生,在尝试比较两个字符时遇到了问题,下面是一个例子:

#define PartOne "He"
#define PartTwo "llo"
char Final1Var[] = PartOne PartTwo;
char ComapreVars[] = "Hello";
if(Final1Var == ComapreVars)//This is were the problem occurs, the chars are supposed to be equal to each other BUT for some reason the 'if' statement ends up determining they're not?
   InGameDialog::Alert("They Match");
else
    InGameDialog::Alert("They Don't Match");

代码出了什么问题?我无法想象为什么这不起作用?有什么建议吗?

c++字符数组中的

无法使用==运算符进行比较,您必须使用strcmp函数或字符串比较函数。

这是一个非常常见的问题。一旦我在其他问题中找到一个好的答案,我就会把它标记为重复。

同时,你不是在比较chars,而是在比较char[]s,这是完全不同的。您将希望使用strcmp、strncmp或std::字符串类型将是更好的解决方案。

什么是数组衰减?对代码中发生的事情以及原因有一些合理的解释。

最新更新