Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 值转换为空字符串_C_Arrays_Pointers - Fatal编程技术网

C 值转换为空字符串

C 值转换为空字符串,c,arrays,pointers,C,Arrays,Pointers,当我用python tutor可视化我的代码时,数组的值变成空字符串,这是在执行if条件时发生的,我不明白为什么会发生这种情况。有人能解释一下为什么会发生这种情况并提供解决方案吗 #include <stdio.h> int countWords(char *src, char toFind) { int numWords; int i; int d; numWords = 0; i = 0; d = 0; while

当我用python tutor可视化我的代码时,数组的值变成空字符串,这是在执行if条件时发生的,我不明白为什么会发生这种情况。有人能解释一下为什么会发生这种情况并提供解决方案吗

  #include <stdio.h>

int countWords(char *src, char toFind)
{
    int numWords;
    int i;
    int d;

    numWords = 0;
    i = 0;
    d = 0;

    while (*src)
    {

      if (*src =! (toFind)) //it happens here*?*
        numwords++;

      src++;
    }  

}

int main()
{
    char arr[] = "abc,ffg,ijk";
    char c = ',';
    int res = 0;
    char *ptr;

    ptr = arr;

    res = countWords(ptr,c);

    printf("%d", res);
    return 0;
}

您输入了一个错误,如果您想进行比较,您应该使用!=而不是=!它是两个运算符,告诉编译器对变量求值并将反向结果赋给*src

此外,假设您想要的是字数而不是字符数,则可能使用了错误的运算符。使用==将解决此问题。

您忘记在函数countWords的末尾添加一个返回

返回numwords;
正如SaltyPleb所说,我认为您的函数返回要查找的字数,您应该更改!=by==。

您没有返回countWords中的任何值。使用返回numWords;