为什么收到警告:指针与整数之间的比较

问题描述 投票:0回答:1

我收到警告讯息,说:

指针与整数之间的比较

char * replaceWord(const char * str, const char * oldWord, const char * newWord)
{
    char * resultString;
    int i, count = 0;
    int newWordLength = strlen(newWord);
    int oldWordLength = strlen(oldWord);
    //count the no of occurance of word in string in a file
    for (i = 0; str[i] !='\0'; i++)
    {
        if (strstr(str[i], oldWord) == str[i])//i m getting warning here
        {
            count++;

            i = i + oldWordLength - 1;
        }

    }

    // Making a new string to fit in the replaced words
    resultString = (char *)malloc(i + count * (newWordLength - oldWordLength) + 1);

    i = 0;
    while (*str!='\0')
    {
        // Compare the substring with result
        if(strstr(str, oldWord) == str)//here i used same syantax its working but not above 
        {
            strcpy(&resultString[i], newWord);
            i += newWordLength;
            str += oldWordLength;
        }
        else{
            resultString[i] = *str;
            i += 1;
            str +=1;
        }
    }
    resultString[i] = '\0';
    return resultString;
}
pointers int
1个回答
0
投票

strstr()的手册页:

#include <string.h>

char *strstr(const char *haystack, const char *needle);

因此,strstr()返回子字符串的char*指针作为结果,并且您正在将其与char变量(str[i])进行比较,从而得出错误。

© www.soinside.com 2019 - 2024. All rights reserved.