Reputation: 365
I keep getting told that in this line of code passing argument from incompatible pointer type.
Here's the line of code:
if (linear_search (size_of_A, argv[i]))
What does this mean and how do I fix it? Here is the whole program:
int linear_search ( int size_of_A, char*argv[]){
int i;
i = 2;
while (i <= size_of_A - 1){
if (!strcmp (argv [1], argv[i])){
return 1;
}
}
return 0;
}
int main (int argc, char*argv []){
int size_of_A = argc - 2;
int i = 2;
if (linear_search (size_of_A, argv)){
printf ("%s not found\n", argv [1]);
return 1;
} else{
printf ("%s found\n", argv[1]);
return 0;
}
i = i + 1;
}
}
Ok, that fixes the warning, but now when I run the program through the compiler nothing happens. It's supposed to tell me if the first argument is repeated or not.
For example the output would look like:
./a 3 hso 8 3
3 found
Upvotes: 2
Views: 10256
Reputation: 27183
oldfart: Beware of strcmp
and all unlimited string functions (like strcpy--but be careful with strncpy: it doesn't add a trailing NULL).
youngun: But it's ok since argv
has a bunch of NULL terminated strings!
of: That way of thinking leads to a heaping helping of buffer overflow. argv
was initialized with NULLS. But it could have changed since then.
y: But this is a small program and it will never happen.
of: Sure. Programs grow and 'never' happens more often than you think.
y: OK, well Mr. Paranoid, what size should I use for the limit on my strncmp
? There's no obvious number to use! Are you suggesting I make something up?
of: See limits.h, in particular ARG_MAX
and _POSIX_ARG_MAX
for possible maximum values. You could use smaller values if you so desire. If you do, document it.
y: So I should write strncmp( arg, target, 5000 )
? OK, mister paranoid.
of: Don't do that! #define APP_MAX_ARG 5000
and then use strncmp( arg, target, APP_MAX_ARG )
. Don't get me started on magic numbers. Kids these days.
of: Hey, kid, get off my lawn.
Upvotes: 0
Reputation: 208353
Your linear search function only compares argv[1] with argv[2] as i is never incremented. A possible solution (using a for loop instead of a while, which is more common) would be:
int linear_search ( int size_of_A, char*argv[]){
int i = 0; // should always initialize in construction
for ( i = 2; i < size_of_A; ++i ) {
if (!strcmp (argv [1], argv[i])){
return 1;
}
}
return 0;
}
Variable i is never really used in main, you can safely remove the two lines that deal with it.
Upvotes: 1
Reputation: 399833
This seems to be great place to use the fact that argv
is
NULL
-terminated. You don't need to pass the count of items in it, but just do like this:
/* Searches through the NULL-aterminated array for the.
* given needle string. Returns 1 if found, 0 if not.
*/
int linear_search (char **argv, const char *needle)
{
int i;
for(i = 0; argv[i] != NULL; i++)
{
if(strcmp(argv[i], needle) == 0)
return 1;
}
return 0;
}
Btw, I recommend comparing the return value of strcmp() nagainst literal 0, rather than using the ! notation, since the return value is in fact not boolean; it returns a int that expresses the relation between the compared elements.
I realize the use of ! here is common enough to be almost idiomatic, since many people think that C must always be as terse as possible, but ... I still recommend against it.
Upvotes: 0
Reputation: 87
I don't know the logic of your program but compiler error is solved. See this:
int main (int argc, char*argv []){
int size_of_A = argc - 2;
int i = 2;
if (linear_search (size_of_A, argv[])){
printf ("%s not found\n", argv [1]);
return 1;
}
Upvotes: 0
Reputation: 43159
You have a logic error in linear_search
. You have a while loop based on the value of i
, but i
never changes. You might be missing a i++;
instruction.
Also (although this won't change your program's behaviour): the variable i
in main
is never really used. You can remove it and the i = i + 1;
instruction.
Upvotes: 2
Reputation: 1487
Your linear_search() accepts an "array of strings" (pointer to array of characters), while you are passing it a "string" (argv[i]). What you want to do I think is to call linear_search as:
if (linear_search (size_of_A, (argv + i)))
Although I do not completely understand the logic of your program... Are you trying to search argv[1] in the later arguments...?
Upvotes: 0
Reputation: 35141
int linear_search ( int size_of_A, char*argv[]){
char*argv[]
means "pointer to an array of char"; as a function call parameter, that's the same as char**
.
argv is a char**
, but argv[i]
is a char*
, because C defines argv[i]
as *(argv + i )
or, in English "dereference (argv plus i)". Dereferencing a char**
leaves you with a char*
, and that''s not what linear_search
is declared to take.
Take a look here if you're still confused.
`
Upvotes: 0
Reputation: 354536
Your function expects a char*[]
(which should be equivalent in this case to char**
). However, by calling it with
linear_search (size_of_A, argv[i])
you are just passing a char*
as argument (since [i]
dereferences one pointer). From what I see in your code you could try using
linear_search (size_of_A, argv+i)
but I'm not quite sure whether that will yield the intended behaviour. Still too early to understand C :)
Upvotes: 0
Reputation: 88355
linear_search is expecting a data type "char **
". You are passing in argv[i]
, which is just a char*
. Try passing in "argv" like this:
if (linear_search(size_of_A, argv))
Upvotes: 4
Reputation: 135285
You are passing a parameter (the second one) to the linear_search
function which does not match the expected type of the argument. argv[i] has the type char *
, which is commonly referred to as a string, while the linear_search
function expects char * []
, which is an array of strings.
Upvotes: 1