将参数与第一个参数匹配
好吧,我必须编写一个接受 2 个或更多参数的程序,并搜索第二个和其余参数以查找匹配的参数。
例如,输出将是:
./a 3 h 4 9 3
3 found
或
./a hsi and iash me 34 hsi
hsi found
到目前为止,我已经有了这个,而且我很确定这里有很多垃圾,在这种情况下毫无用处。 任何提供的帮助将不胜感激!:
int linear_search (const char*A[], char*x, int v ){
int i;
i = 0;
while ( i < v - 1){
if (A[i] == x){
return 1;
}
return 0;
}
}
int main (int argc, char*argv[]){
int size = argc - 1;
char*A[size];
char*x = argv [1];
int i;
int v = argc - 2;
i = 0;
while ( i < v ){
A[i] = argv [i + 1];
i = i +1;
}
if (linear_search (A, v, x)){
printf ("%s found\n", x);
} else {
printf ("%s not found\n", x);
}
}
每当我通过编译器运行程序时,我都会收到警告:从不兼容的指针类型传递“线性搜索”的arg 1。
警告:传递 'linear_search' 的 arg 2 会使指针来自整数而不进行强制转换。
这意味着什么?
Okay I have to write a program that accepts 2 or more arguments and searches the second and remaining arguments for a matching argument.
for example the output would be:
./a 3 h 4 9 3
3 found
or
./a hsi and iash me 34 hsi
hsi found
So far I have this, and I'm pretty sure I've got a lot of junk in here that is useless in the situation. Any help provided would be greatly appreciated!:
int linear_search (const char*A[], char*x, int v ){
int i;
i = 0;
while ( i < v - 1){
if (A[i] == x){
return 1;
}
return 0;
}
}
int main (int argc, char*argv[]){
int size = argc - 1;
char*A[size];
char*x = argv [1];
int i;
int v = argc - 2;
i = 0;
while ( i < v ){
A[i] = argv [i + 1];
i = i +1;
}
if (linear_search (A, v, x)){
printf ("%s found\n", x);
} else {
printf ("%s not found\n", x);
}
}
Whenever I run the program through the compiler I get the warning: passing arg 1 of 'linear_search' from incompatible pointer type.
warning: passing arg 2 of 'linear_search' makes pointer from integer without a cast.
What does that mean?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我就是这样做的。 您不需要单独的线性搜索函数。
Here's how I'd do it. You don't need a separate linear search function.
我认为问题出在线性搜索函数中......看起来您只是在比较指针以查看它们的字符串是否相同。
C 不是这样工作的。 所做的只是检查指针地址是否相同。 您需要使用 strcmp() 函数来检查实际字符串是否相同。
我还建议对变量进行更具描述性的命名,这使得事情更容易阅读。 :)
I think the problem is in the linear search function... It looks like you are just comparing the pointers to see if they strings are the same.
C doesn't work like that. All that does is check to see if the pointer addresses are the same. You need to use the strcmp() function to check that the actual strings are the same.
I also recommend working on naming your variables a little more descriptively, it makes things much easier to read. :)
linear_search
的调用与声明不匹配。 如果不是彻底的错误,这至少应该给你警告。声明是:
线性搜索(const char*A[],char*x,int v)
而调用是:
线性搜索(A,v,x)
最后两个参数确实应该交换。
此外,您不能使用
==
运算符来匹配 C 中的字符串。您必须使用strcmp
、strncmp
或 < code>memcmp.`如果您打算使用索引
2< 中的
A
,您可能需要开始复制/code> 而不是第一个(argv[ 1 ]
是您要搜索的键,将其放入A
中将始终返回匹配项,即使它不存在于任何地方else 在参数列表的其余部分中)。请注意,在 C 语言中,您可以使用下标运算符将数组的一部分传递给函数,因此不需要复制到数组
A
。 您可以将&argv[ 2 ]
作为linear_search
的第一个参数。The invocation of
linear_search
does not match the declaration. This should at least give you warnings if not outright errors.The declaration is:
linear_search (const char*A[], char*x, int v )
whereas the invocation is:
linear_search (A, v, x)
The last two arguments should really be swapped.
Also you cannot use the
==
operator to match strings in C. You will have to use one ofstrcmp
,strncmp
ormemcmp
.`You probably need to start copying, if you plan to use
A
from the index2
and not the first (theargv[ 1 ]
is the key you are searching for, putting it inA
will always return a match even if it's not there anywhere else in the rest of the argument list).Note in C, you can use the subscript operator to pass part of the array to the function, so you don't need the copy to the array
A
. You could have just done&argv[ 2 ]
as the first parameter oflinear_search
.