简体   繁体   English

C - 指针与 integer('char *' 和 'int')之间的比较

[英]C - comparison between pointer and integer ('char *' and 'int')

I'm very new at C and I'm having a problem using function strstr .我是 C 的新手,使用 function strstr时遇到问题。 Make gives me a warning that I do not understand, anybody can explain? Make 给我一个我不明白的警告,任何人都可以解释一下吗?

warning: comparison between pointer and integer ( char * and int ) if (strstr(reference, number_start) == 1) warning generated.警告:指针与 integer( char *int )之间的比较if (strstr(reference, number_start) == 1)生成警告。

My code:我的代码:

#include <stdio.h>
#include <string.h>


int main (void) {
    char reference[]        = "34,37";
    char number[]           = "34567890123456";
    char number_start[]     = {number[0], number[1], '\0'};

    if (strstr(reference, number_start) == 1) {
        printf("Yup, iit's workin'\n");
    } else {
        printf("Try again, wookie\n");
    }

    return 0;
}

strstr() return a pointer to the first substring if found, and NULL if not found. 如果找到,则strstr()返回指向第一个子字符串的指针,如果找不到,则返回NULL

Replace 更换

if (strstr(reference, number_start) == 1) {

with

if (strstr(reference, number_start) != NULL) {

to check if number_start is in reference . 检查number_start是否在reference

Do int(strstr(a,b)) if strstr is not null for index.如果索引的 strstr 不是 null,则执行int(strstr(a,b))

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

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