c4133警告c代码

0 c string pointers strstr

我有一个问题,警告c4133,它说问题是从char*到int*的无法使用的类型,我尝试施放指针((char*)x)但没有运气,也许有人知道什么是问题/

这是我的程序,功能中的问题.

void replaceSubstring(char *str, char *substr)//function that gets string and substring and      make the substring in string big letters
{
    int i;
    int  *x;

    x = (strstr(str, substr));//The problem line 
    while (strstr(str,substr) != NULL)
    {
        for (i=0;i<strlen(substr);i++)
        {
            *x = *x - 32;
            x++;//move to the next char
        }
        x = (strstr(str, substr));  //first apear of substr int str
    }
 }
Run Code Online (Sandbox Code Playgroud)

Sou*_*osh 7

在你的代码中,x被定义为int *但是返回类型strstr()char *..在这里查看手册页.

值得一提的是,casting通常被认为是一种不好的做法c.通过正确编写代码,cast可以避免大多数情况.大多数情况下,castING 引入大量的bug.仔细检查数据类型并远离casting.

旁注:只是一个建议,也许之前直接减去32*x,也许你想执行一个范围检查*x是内97-122以确保是小写字母.

此外,最好strlen(substr)在循环外执行,存储到变量中并在循环中使用该值.将为您节省冗余呼叫的开销strlen().