匹配参数的第一个参数 [英] Match arguments to first argument

查看:111
本文介绍了匹配参数的第一个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我必须写一个接受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

./a hsi and iash me 34 hsi  
hsi found

到目前为止,我都这样了,我是pretty相信我已经得到了很多垃圾在这里是在形势没用。提供任何帮助将大大AP preciated!:

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);  
    }  
}

每当我通过编译器运行程序我得到警告:从通过兼容的指针类型'linear_search的ARG 1

警告:通过linear_search的ARG 2时将整数指针,未作投

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.

这是什么意思?

推荐答案

下面就是我会做。你并不需要一个单独的线性搜索功能。

Here's how I'd do it. You don't need a separate linear search function.

#include <stdio.h>
#include <string.h>
int main (int argCount, char *argVar[]) {
    int i;
    if (argCount < 3) {
        fprintf (stderr, "Usage: argfind <argToFind> <otherArg> ...\n");
        return 1;
    }
    for (i = 2; i < argCount; i++) {
        if (strcmp (argVar[1], argVar[i]) == 0) {
            printf ("'%s' found in argument %d\n", argVar[1], i);
            return 0;
        }
    }
    printf ("'%s' not found\n", argVar[1]);
    return 0;
}

这篇关于匹配参数的第一个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆