【问题标题】:choosing between function returns在函数返回之间进行选择
【发布时间】:2018-05-29 03:39:46
【问题描述】:

我写了一个函数,它有两个不同的返回选项:

//function to compare the strings in function3
int compare(char* str, char* dest){
  int answer;
  int i;
 int length;
  length = strlen(str);
  // int jlength;
  // jlength = strlen(dest);
  for(i=0; i<length; i++){
    if(str[i] == dest[i]){
      answer = 1;
    }else {
      answer = 2;
    }
  }
  return answer;
}

我想稍后使用这个函数,并且根据函数返回的内容发生不同的事情。以下是我如何构建的相关部分:

 //compare the reversed str with the orignal, now stored in dest
  compare(str, dest);
  int function3answer;
  if(compare == 1){
    function3answer = 1;
  }else{
   function3answer = 2;
  }
  return function3answer; 
}

编译时出现错误:

warning: comparison between pointer and integer [enabled by default]

在 1 周围添加单引号没有帮助(也不是我真正想要的,因为我没有引用数组的一部分),也没有将其降低为一个等号(这会产生不同的警告)。

非常感谢!

【问题讨论】:

  • 第一个错误:将函数 compare 的返回值分配到 if-else 语句之前的变量中。
  • 我不明白。你在尝试这个if(compare(str, dest) == 1){ 吗?还是function3answer = compare(str, dest);
  • 另外,如果你在比较字符串,你的 compare 函数很容易被 strcmp 替换,这是不正确的。
  • @woz 谢谢你,你的第一条评论成功了!谢谢,但是比较是一项更大任务的一部分,因此编写我认为的函数更容易。非常感谢您的帮助!
  • @eids 考虑这种情况:str1 = abcd 和 str2 = xyzd。由于您在每次迭代中逐个字母地比较两个字符串并覆盖变量 answer 的值,因此在您的情况下唯一真正重要的迭代是最后一个。在我提到的情况下,answer 将是 1,因为最后一个字母是相同的,但是 str1 != str2。

标签: c function if-statement return-value


【解决方案1】:

错误

warning: comparison between pointer and integer [enabled by default]

来自这一行:

if(compare == 1){

您尝试将函数整数进行比较。

要消除此错误,请使用 compare 更改函数:

void some_function(...) {
    //compare the reversed str with the orignal, now stored in dest
    int compare_result = compare(str, dest);

    int function3answer;

    if(compare_result == 1){
        function3answer = 1;
    }else{
        function3answer = 2;
    }
    return function3answer; 
}

现在,compare 函数。你实现它的方式不会像你期望的那样工作:

  • 如果你比较"abc""poc",你会在for循环:

    i = 0, str[0] == 'a', dest[0] == 'p' ==> answer = 2
    i = 1, str[0] == 'b', dest[0] == 'o' ==> answer = 2
    i = 2, str[0] == 'c', dest[0] == 'c' ==> answer = 1
    i = 3, going out for loop and returning **1**.
    
  • 最糟糕的是,如果您将"a long string""tiny" 进行比较,当i4 时,您将得到一个UB。

您可以通过这种方式更正 compare 函数:

#define STRING_IDENTICAL 1
#define STRING_DIFFERENT 2

//function to compare the strings in function3
int compare(char* str, char* dest)
{
    int answer;
    int i;
    int length;
    length = strlen(str);
    int jlength;
    jlength = strlen(dest);

    if (length != jlength)
    {
        /* since size are differents, string are differents */
        return STRING_DIFFERENT;
    }

    for(i=0; i<length; i++){
        if(str[i] != dest[i]){
            /* at least one different character, string are differents */
            return STRING_DIFFERENT;
        }
    }

    /* if we reach this point, that means that string are identical */
    return STRING_IDENTICAL;
}

【讨论】:

  • 这完美地解释了一切,非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-11
  • 1970-01-01
  • 1970-01-01
  • 2013-02-23
  • 2021-09-30
相关资源
最近更新 更多