为什么这不匹配
Why does this not match?
为什么这不匹配?
...
puts (ep->d_name);
if(ep->d_name=="testme"){ printf("ok"); } else { printf("no"); }
...
输出:
testme
no
尝试:
if(!strcmp(ep->d_name, "testme"))
或者d_name
改为string
。
发生这种情况是因为您正在比较两个指针,这两个指针指向具有相同值的 char*
你真的应该做
puts (ep->d_name);
if(strcmp(ep->d_name, "testme")==0){
printf("ok");
}
else {
printf("no");
}
虽然请考虑使用字符串,因为这会给你所需的语义
http://en.cppreference.com/w/cpp/string/basic_string
我们需要知道d_name传递了什么值。
要使程序打印"ok",该值也需要为"testme"。
另外,看看这个函数:strcmp。 它比较两个字符串,这基本上就是您在这里所做的。
例:
/* strcmp example */
#include <stdio.h>
#include <string.h>
int main ()
{
char szKey[] = "apple";
char szInput[80];
do {
printf ("Guess my favourite fruit? ");
gets (szInput);
} while (strcmp (szKey,szInput) != 0);
puts ("Correct answer!");
return 0;
}
相关文章:
- 为什么我收到错误:"运算符<<不匹配?
- 模板推导:为什么函数指针模板定义在常量和/或引用时不匹配?
- 为什么我的数组值与此处的全局变量不匹配?
- 为什么使用不匹配的参数调用重载函数仍然有效
- 没有函数模板的实例与我不知道为什么的参数列表匹配
- 为什么一次包装 typedef 函数签名与原始签名不匹配
- 作为模板类型参数,为什么 type[N] 与其专用版本不匹配----模板<类 T>类 S<T[]>
- 如果在开关情况下不匹配,为什么此函数返回 ASCII 值?
- C STD Regexp为什么不匹配
- 为什么这个"uint64_t"模板专用化与 Mac 平台上的"无符号长"不匹配?
- 不匹配新[]和删除,为什么没有内存泄漏
- 为什么在使用"std::find"时,错误与"运算符=="不匹配?
- 为什么此代码编译 (C++11) 而没有类型不匹配错误
- 为什么regex_match与我的正则表达式不匹配
- Curly Braces构造函数更喜欢initializer_list而不是更好的匹配.为什么
- 为什么构造函数的数量与Destructor调用的数量不匹配
- 为什么我们有一个类型不匹配
- 为什么在列表initialization函数参数时,显式构造函数不匹配
- 为什么我没有警告枚举比较不匹配
- 为什么迭代器类型与矢量容器中的 value_type * 不匹配