Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.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_Function_Return Value - Fatal编程技术网

C 检查双字符-函数

C 检查双字符-函数,c,function,return-value,C,Function,Return Value,我已经创建了一段代码,可以检查输入文本中的双字符。如果我把所有代码都放在主函数中,我就可以工作了,但是当我想创建一个额外的函数时,我会遇到一些麻烦。我得到的错误如下:“错误:控件可能到达非void函数的末尾”,我已确定系统无法识别count\u double\u characters函数的返回值 你能帮我理解我做错了什么吗 #include <stdio.h> #include <string.h> int count_double_characters(char *c

我已经创建了一段代码,可以检查输入文本中的双字符。如果我把所有代码都放在主函数中,我就可以工作了,但是当我想创建一个额外的函数时,我会遇到一些麻烦。我得到的错误如下:“错误:控件可能到达非void函数的末尾”,我已确定系统无法识别count\u double\u characters函数的返回值

你能帮我理解我做错了什么吗

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

int count_double_characters(char *ch);

int main(void)

{
char input[400];
printf("Write the text you want to check: ");
fgets(input, sizeof(input), stdin);
count_double_characters(input);
}   


int count_double_characters(char *ch)
{
char n = strlen(ch);  
int count_double = 0;

for (int i = 0; i < n; i++)
{
    for (int j = i + 1; j < n; j++)
    {
        if (ch[i] == ch[j])
        {
            count_double++;
        }
    }

}

if (count_double > 0)
    {
        char s = printf("Its a double!\n");
        return s;
    }
    
else if (count_double == 0)
    {
        char d = printf("Looks good\n");
        return d;
    }
}
#包括
#包括
整数计数双字符(字符*ch);
内部主(空)
{
字符输入[400];
printf(“写下要检查的文本:”;
fgets(输入,sizeof(输入),标准输入);
计数双字符(输入);
}   
整数计数双字符(字符*ch)
{
charn=strlen(ch);
int count_double=0;
对于(int i=0;i0)
{
char s=printf(“它是双精度的!\n”);
返回s;
}
else if(count_double==0)
{
char d=printf(“看起来不错”\n);
返回d;
}
}

考虑代码的这一部分:

if (count_double > 0)
    {
        char s = printf("Its a double!\n");
        return s;
    }
    
else if (count_double == 0)
    {
        char d = printf("Looks good\n");
        return d;
    }

  // if count_double is less than 0, the program goes here
  // but there is non return statement, meaning that the function
  // does not return any value.
  // That what's the error message is telling you
}
现在,您将告诉我,
count\u double
永远不能为0,这是正确的,但显然编译器不够聪明,无法检测到这一点


要更正,只需删除
if(count\u double==0)
或用
if替换即可问题是:你有一个
if
块返回一个值,一个
else if
块也返回一个值,但是你没有一个最终的
else
块返回一个值,换句话说,你需要覆盖所有情况。谢谢!我得到了代码:)使用
字符输入[400]
在更高级别的代码中,
charn=strlen(ch);
是不可靠的。推荐
size\t n=strlen(ch);
谢谢你的回答。我可以看出这更有意义:)谢谢!非常感谢你!太蠢了,我竟然没想到!我现在可以使用代码:)