简体   繁体   中英

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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