例如,要验证有效的Url,我想执行以下操作
char usUrl[MAX] = "http://www.stackoverflow"
if(usUrl[0] == 'h'
   && usUrl[1] == 't'
   && usUrl[2] == 't'
   && usUrl[3] == 'p'
   && usUrl[4] == ':'
   && usUrl[5] == '/'
   && usUrl[6] == '/') { // what should be in this something?
    printf("The Url starts with http:// \n");
}
或者,我考虑过使用strcmp(str, str2) == 0,但这一定非常复杂.
是否有标准的C函数可以做这样的事情?
使用以下 c 宏替换 strncmp(可能还有任何其他函数)中的参数似乎存在问题:
#define STRING_WITH_SIZE_MINUS_ONE(x)   x, sizeof(x)-1
...
if (0 == strncmp(str1, STRING_WITH_SIZE_MINUS_ONE("str2")) {
// do something
}
当使用arm-linux-gnueabihf-gcc 4.9.2构建时,这会抛出错误error : macro "strncmp" require 3 argument, but only 2给定的。使用arm-linux-gnueabihf-gcc 8.3.0 构建似乎工作得很好。
显然,整个 strncmp 函数可以放在宏内部,并且它可以很好地工作,但是理解为什么它不能编译是非常有益的。
我在C中编写一个非常简单的函数来检查字符串是绝对路径还是相对路径.无论我尝试什么,它总是返回false.
这是我尝试过的:
int isAbsolute(char *str){
    if(strcmp(str,"/")){
        return 1;
    }
    return 0;
}
我称之为:
printf("%d\n", isAbsolute("/"));
每次都返回false.很明显,我错过了一些明显的东西,但我无法弄明白......