PCSTR比较VC ++ [英] PCSTR Comparison VC++

查看:68
本文介绍了PCSTR比较VC ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须比较两个指向两个单独字符串的PCSTR指针-我知道这些字符串是相等的,但是由于某种原因,该比较永远不会产生所需的true.

I have to compare two PCSTR pointers pointing to two separate strings - I know for sure the strings are equal but for some reason the comparison never results in the desired true.

PCSTR one = "hello";
PCSTR two = "hello";

if(one==two)
{

MessageBoxA(0,"True","Message",0);
}



有什么想法吗?

OP的其他信息是从下面的评论中复制的
我尝试的是使用例如strstr和strcmp,但都没有成功.我还尝试将字符串转换为十六进制,以确保没有隐藏的字符或类似的字符使它们不相等,但生成的十六进制字符串也相等.



Any ideas on this?

OP''s additional information copied from comment below
What I tried was using strstr and strcmp for example which did not succeed as well. I also tried to convert the strings to hex to make sure there are no hidden characters or something like that making them not equal but the resulting hex strings are equal as well.

推荐答案

您是说PC T STR吗?对于此数据类型,请使用lstrcmpi()函数.
请查看此链接以获取详细信息:

http://msdn.microsoft.com/zh-CN /library/windows/desktop/ms647489(v=vs.85).aspx [
Do you mean PCTSTR? For this datatype use lstrcmpi() function.
Please see this link for details:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms647489(v=vs.85).aspx[^]


您不比较字符串,而是比较指针,指向不同的字符串(尽管实际内容是相同的).您应该使用字符串比较函数之一 [
You are not comparing the strings, you are comparing the pointers, which are pointing to different strings (although the actual content is the same). You should be using one of the string comparison functions[^].


报价:

PCSTR one ="hello";
PCSTR两个="hello";

if(one == two)

PCSTR one = "hello";
PCSTR two = "hello";

if(one==two)


应该是


That should be

PCSTR one = "hello";
PCSTR two = "hello";
 
if(strcmp(one,two)==0)



如果您不信任strcmp函数(或者只是想找到其他字符),那么您可以编写自己的比较函数,毕竟这是一个简单的迭代.



If you don''t trust strcmp function (or simply want to locate the different character) then you may code your own comparison function, it is a simple iteration, after all.


这篇关于PCSTR比较VC ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆