这是我到目前为止编写的代码,但是我想用int myStrStr代替char myStrStr但是当我用int代替char时,它给了我一个错误。请帮忙。

int my_strlen(const char *s1) {
    int i;
    for (i = 0; *s1; s1++, i++);
    return i;
}

char myStrStr(char * haystack, char * needle){
    int i;
    if(*needle =='\0')
        return (char *)haystack;
    else{
        for(;*haystack; haystack++){
            if(*haystack == *needle){
                for(i=1;*(haystack +i)==*(needle +i);i++);
                if(i==my_strlen(needle))
                    return (char *)haystack;
            }
        }
        return NULL;
    }
}

最佳答案

因为您返回char*NULL。您不能将指针转换为非指针类型。
现在在编译时应该是错误的。您的函数应返回char*

您不能将char*强制转换为int*,因为int在内存中分配了更多空间,因此必须重新分配内存,这必须手动完成。

关于c - 创建StrStr()函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55005856/

10-10 17:27
查看更多