所以我在比较存储在链接列表中的字符串时遇到问题。链表中的所有值都存储为 Void*,但我不太确定如何将 void* 与字符串进行比较。我尝试过使用 strcmp((,但没有运气。有人可以指出我正确的方向吗?谢谢。
链接列表类:
typedef struct MissileNode
{
//It can store any data types
void* missile;
struct MissileNode* next;
}missile_node_t;
主类:
missile_node_t* current = missiles->head;
//This totally prints perfectly
printf("Current Missile: %snn", current->missile);
//This is where I am having issue, my comparsion is not working
if(strcmp((char*)current->missile),"Single") == 0)
{
printf("work");
}
else
{
printf("doest work");
}
current = current-> next;
输出:
Current Missile: Single
doesn't work
该函数定义为int strcmp (const char* str1, const char* str2);
。 因此,您需要提供两个字符串参数。
在代码中,您编写:
if(strcmp(current->missile),"Single") == 0)
所以你只用一个参数来调用strcmp(current->missile)
。你可能的意思是strcmp(current->missile, "Single")
.
如果您使用的是 C 编译器,则无需强制转换即可工作。如果您使用的是C++编译器,则需要使用strcmp(reinterpret_cast<char*>(current->missile), "Single")