views:

196

answers:

3

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  A: 

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;
}
paxdiablo
We have not been taught or are using a lot of what you used...thanks though!
Kaity
You should have labeled your question with the homework tag if it's homework. Otherwise you'll get answers rather than guidance. But the code above shows your problem: you need to compare strings with strcmp, not ==. == just compares the pointers, not what they point to.
paxdiablo
A: 

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

Nathan Reed
how would I use strcmp()? would I say A[i] strcmp (x) ????
Kaity
@Kaity: "if (strcmp (A[i],x) == 0) {...}" - an example is shown in my code above (for now, it may be below in the future :-). strcmp() is a function that takes two char*'s and returns 0 for equal or -1, +1 for arg1<arg2, arg1>arg2 respectively. You only need to worry about 0 in your case.
paxdiablo
A: 
  • 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.

dirkgently