【问题标题】:search function double return value possible搜索功能双返回值可能
【发布时间】:2018-12-02 18:17:11
【问题描述】:

我正在编写一个搜索功能。参数是:

(char *array, char to_find)

char search (char *array, char to_find)
{
int counter;
for (counter =0; array[counter]!='\0'; counter++)
{ if (to_find==array[counter])
  return 2; 
else return 0;
}
}

int main()
{
char *word[100]="woman";
char letter;
scanf("%c" &letter);
if (search(word, letter)==1)
{ 
printf("match")}

}

我想知道是否也可以修改此代码以返回 2 个值;如果为真,则返回 1 & 计数器。例如我尝试过:

    char search (char *array, char to_find)
    {
    int counter;
    for (counter =0; array[counter]!='\0'; counter++)
    { if (to_find==array[counter])
      return 2; 
    else return 0;

    }
    }
char searchindex(char *array, char to_find)
{ int counter;
for (counter=0; array[counter]!='\0'; counter++)
{
if (to_find==array[counter])
{
return counter;
}
else return -1;
}



int main()
    {
    char *word[100]="woman";
    char letter;
    int position;
    scanf("%c" &letter);

    /**I tried representing the return values as integers**/
int test1= search(word, letter);
int test2= searchindex(word, letter);
if ((test1+test2)>0) 
{printf(match);} /**this had errors but i can't seem to find them**/
    }

【问题讨论】:

    标签: c


    【解决方案1】:

    首先注意到您的if-else 声明似乎很奇怪。你有:

    char search (char *array, char to_find)
    {
        int counter;
        for (counter =0; array[counter]!='\0'; counter++)
        { 
            if (to_find==array[counter])
                return 2; 
            else return 0;  // This strange....
        }
    
    }
    

    这意味着您将始终在第一次比较后返回。你可能想要:

    char search (char *array, char to_find)
    {
        int counter;
        for (counter =0; array[counter]!='\0'; counter++)
        { 
            if (to_find==array[counter])
                return 2; 
        }
        return 0;
    }
    

    然后你问:

    ...可以修改此代码以返回 2 个值

    不,在 C 中只能直接返回一个值。但是,有多种方法可以“返回”多个值。

    例如,通过将指针传递给变量,然后使用指针更改值。喜欢:

    char search (char *array, char to_find, int* counter)
    {
        for (*counter = 0; array[*counter]!='\0'; (*counter)++)
        { 
            if (to_find==array[*counter])
                return 2; 
        }    
        return 0;
    }
    
    // Call it like
    int counter;
    int test1= search(word, letter, &counter);
    

    另一种方法是创建一个结构并返回该结构。喜欢:

    struct X {
        int result;
        int counter;
    };
    
    struct X search (char *array, char to_find)
    {
        struct X res;
        for (res.counter = 0; array[res.counter]!='\0'; res.counter++)
        { 
            if (to_find==array[res.counter])
            {
                res.result = 2;
                return res;
            } 
        }    
        res.result = 0;
        return res;
    }
    
    // Call it like
    struct X test1= search(word, letter, &counter);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-03
      • 1970-01-01
      • 2023-04-03
      • 2016-01-12
      • 1970-01-01
      相关资源
      最近更新 更多