1
votes

I am new to C programming. I am trying to compare a two strings. I get the error: Multiple markers at this line. passing argument 1 of 'strcmp' makes pointer from integer without a cast. passing argument 2 of 'strcmp' makes pointer from integer without a cast

char names[SIZE][LENGTH];   
char srch_name[LENGTH];

if(strcmp(names[g][LENGTH], srch_name[LENGTH]) == 1)
3
if(strcmp(names[g], srch_name) == 1) will eliminate the warning, but note that if strings are equal, the return value is 0. - Ken Y-N
Both of the arguments you're passing to strcmp are char elements from arrays. They have an integer type. The arguments should have type char *. Further, you are using LENGTH as an index into these arrays, which is out of bounds. You really need to read a tutorial on C arrays and pointers before trying to code them. The trial-and-error approach simply will not work. - Tom Karzes

3 Answers

3
votes

The immediate problem is that you are passing a pair of chars to strcmp, rather than char*. This is easy to fix - this call will compile:

strcmp(names[g], srch_name)

A second problem, however, is that you do not check strcmp for a specific value, except zero: it returns a value less than zero, zero, or a number greater than zero depending on the comparison result.

  • <0 when the first character that does not match has a lower value in ptr1 than in ptr2
  • 0 when the contents of both strings are equal,
  • >0 when the first character that does not match has a greater value in ptr1 than in ptr2

Note: Since C indexes arrays 0..LENGTH-1, dereferencing an array at its actual length as in srch_name[LENGTH] is almost always an error. The only situation when it is not an error is when you take the address of one-past-end element.

2
votes

strcmp()'s prototype is

int strcmp(const char *s1, const char *s2);

but both the parameters you gave are of type char and not char *.

Perhaps you meant

if(strcmp(names[g], srch_name) == 1)

And if you are checking if the strings are equal, note that strcmp() returns 0 if both strings have the same content.

1
votes
char names[SIZE][LENGTH];

names is an array of arrays of characters (char **).

Looking up an item in that array:

names[g]

is therefore an array of characters (char *). Looking up an item in that:

names[g][LENGTH]

gets you a character (char). And one that's out of bounds of the array at that, since the array's valid indexes only go from 0 to LENGTH - 1. strcmp needs you to pass an array of characters (char *), not a single character (char).

Solution: Get rid of the second lookup.

(also, the result of strcmp needs to be compared to 0, not 1)