比较两个指针所指向的
comparing what two pointers point to
我正在写一个检测网络钓鱼的程序。我试图检查如果URL的基础,如果它是相同的标签或不是。例如在http://maps.google.com"> www.maps.yahoo.com我试图检查URL的最后2部分是否相同,即如果google.com = yahoo.com或不。
我使用下面的代码:
void checkBase(char *add1, char *add2){
char *base1[100], *base2[100];
int count1 = 0, count2 = 0;
base1[count1] = strtok(add1, ".");
while(base1[count1] != NULL){
count1++;
base1[count1] = strtok(NULL, ".");
}
base2[count2] = strtok(add2, ".");
while(base2[count2] != NULL){
count2++;
base2[count2] = strtok(NULL, ".");
}
if((base1[count1-1] != base2[count2-1]) && (base1[count1-2] != base2[count2-2])){
cout << "Bases do not match: " << endl
<< base1[count1-2] << "." << base1[count1-1] << " and "
<< base2[count2-2] << "." << base2[count2-1] << endl;
}
else{
cout << "Bases match: " << endl
<< base1[count1-2] << "." << base1[count1-1] << " and "
<< base2[count2-2] << "." << base2[count2-1] << endl;
}
}
我不确定我在if语句中的比较是否正确。我传入了两个URL。由于
这是比较两个指针char*(正如你指出的;))
base1[count1-1] != base2[count2-1])
用这个代替
strcmp(base1[count1-1], base2[count2-1]) != 0
你可以使用std:string和boost标记器(现在c++ 11)
对
不能通过比较地址来比较字符串,两个相同的字符串可以存储在不同的地址中。要比较它们,你应该输入:
if(strcmp(base1[count1-1], base2[count2-1]) != 0 ||
strcmp(base1[count1-2], base2[count2-2])!=0){
std::cout << "Bases do not match: " << std::endl
<< base1[count1-2] << "." << base1[count1-1] << " and "
<< base2[count2-2] << "." << base2[count2-1] << std::endl;
}
你可以用c++工具做类似的事情:
void checkBase(std::string a1, std::string a2){
size_t a1_start = a1.rfind('.'), a2_start = a2.rfind('.');
a1_start = a1.rfind('.', a1_start-1);
a2_start = a2.rfind('.', a2_start-1);
std::string h1 = a1.substr(a1_start+1), h2 = a2.substr(a2_start+1);
if (h1 == h2)
std::cout << "same" << std::endl;
else
std::cout << "not same" << std::endl;
}
相关文章:
- int数据类型的指针指向的是什么,如果是一个类的私有数据成员,我们创建了该类的两个对象?
- 创建一个棋盘格或"Interweave"两个链接列表。IE 更改两个链表的指针
- 是否可以使用非常量指针调用非常量函数,以及当两个unique_ptrs指向同一个对象时程序的行为方式?
- C++两个对象,其中包含指向同一数组不同部分的指针
- 比较两个 constexpr 指针不是 constexpr?
- 在两个.cpp文件之间定义全局类/结构指针
- 两个抽象类,派生自同一个基类.如何访问从一个抽象类到另一个抽象类的指针
- 如何在单个链表中交换两个节点的位置,只修改指针
- 如何仅考虑 *指针中的前两个元素
- 如果将两个相同的指针作为输入传递,memcmp 会做什么?
- 一个对象的两个指针.删除了一个指针,对象仍然存在
- C++如何同时删除位于两个向量中的 2 个指针?
- 如何将值添加到嵌套结构中,该结构在C++中有两个指针
- 比较两个指针时">="运算符的奇怪行为
- 根据下面的作者,如果两个指针指向不同的数组,则比较的第一个版本将未定义
- 有没有更好的方法来添加两个智能指针?
- 为什么我不能在同一行中定义两个相同类型的类的成员指针
- 为什么 fdump-class-hierarchy 为虚函数提供了两个指针 int vtable
- 两个类别的两个类,有指向另一堂课的指针
- 错误 :"+" 无法添加两个指针