Match arguments to first argument

自闭症网瘾萝莉.ら 提交于 2020-01-15 09:26:28

问题


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?


回答1:


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



回答2:


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.

if (A[i] == x) 

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. :)




回答3:


  • 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 of strcmp, strncmp or memcmp.`

  • You probably need to start copying, if you plan to use A from the index 2 and not the first (the argv[ 1 ] is the key you are searching for, putting it in A 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 of linear_search.



来源:https://stackoverflow.com/questions/750265/match-arguments-to-first-argument

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!