【发布时间】:2021-03-27 04:16:18
【问题描述】:
我一直在尝试为我们的分组完成程序的一部分,但每当我尝试编译时,这部分代码总是出错。
int
Binary_Search(String key, String Name[], int nElements)
{
/* Implement the body of this function. */
int position;
int begin = 0;
int end = String Name - 1; //Error: Expected expression before 'String'
int result = 0;
while(begin <= end)
{
position = (begin + end) / 2;
if(result = strcmp(String Name, nElements) == 0) //Error: Expected expression
//before 'String'
// too few arguments to
// Function 'strcmp'
return position;
else if(result < 0)
begin = position + 1;
else
end = position - 1;
}
}
【问题讨论】:
-
什么是
String?你从哪里得到错误?请edit您的问题不仅包括完整和完整的错误输出,复制粘贴为文本,还请尝试包括导致错误的minimal reproducible example,并在您获得它们的行上添加cmets。 -
作为关于您的问题的提示,请返回您的教科书(如果没有,请查阅)并阅读有关数组和指针的更多信息。
-
对不起@Someprogrammerdude,我对编程很陌生,而且通过在线课程,我学习编程有点困难
-
strcmp(String Name, nElements)的两个概率 a) 传递参数时不能提及类型,b) 第二个参数也必须是字符串,而不是整数。 -
另一个问题是分配给
result的布尔值。它不能是< 0,因为在下面的测试中(尽管来自strcmp()的返回值可以)。我认为你应该多研究strcmp()。
标签: c string binary-search strcmp