1
我想查看一个字符串是否等同于一个URL路径。它应该很简单,但strcmp总是返回< 0(-47)。我可能在斜杠上做错了什么。C使用strcmp来检查URL路径
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
int main()
{
char path[9];
strcpy(path, "/my/path/");
int len = strlen(path);
char lastChar[1];
strcpy(lastChar, &path[len - 1]);
printf("LAST CHAR SET TO %s\n", lastChar);
bool isPageRequest = strcmp(lastChar, "/") == 0;
if(isPageRequest)
{
printf("ITS A PAGE REQUEST\n");
}
bool isMyPath = strcmp(path, "/my/path/") == 0;
if(isMyPath)
{
printf("ITS MY PATH PAGE\n");
}
return 0;
}
我期待ITS MY PATH PAGE
打印出来..但它没有。
我明白这一点。我不明白的是为什么这会使代码的其余部分工作.. –
@JDoe .:未定义的行为是不正当的,它可能会让你觉得你修复了一个bug,然后让你想知道如何? – chqrlie