简体   繁体   English

如何使用 strcmp() 将字符串与两个字符进行比较,有没有合适的方法来做到这一点?

[英]How to compare a string with two characters using strcmp(), is there a proper way to do that?

I am trying to use the strcmp() to compare the two characters \r\n with a string, but I am getting the following error.我正在尝试使用 strcmp() 将两个字符 \r\n 与字符串进行比较,但出现以下错误。

int parse_lines(char * mem) {
    int count = 0;
    int i = 0;
    char * mmm = "\r\n";
    while (mem[i] != '\0') {
        if (strcmp(mem[i], mmm) == 0) {
            count++;
            mem[i] = '\0';
        }
        i++;
    }
    //check if there is a newline before the null character...
    if (i > 0 && (strcmp(mem[i - 1], mmm) == 0)) {
        mem[i - 1] = '\0';
        count--;
    }
    return count + 1;
}

在此处输入图像描述

Change the following:更改以下内容:

strcmp(mem[i], mmm)
strcmp(mem[i - 1], mmm)

To:至:

strcmp(&mem[i], mmm)
strcmp(&mem[i - 1], mmm)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM