Jason M.
Jason M.

Reputation: 702

Using "strcmp" on specific members of a character array in c

I have a binary search function I am passing a pointer character array, the length of that array, a search pointer character array and another counter for something else.

int binarySearch(char* charArray, int len, char* searchItem, int counter)
{
    int position;
    int begin = 0;
    int end = len-1;
    int cond =0;


    while(begin <= end)
    {

    position = (begin + end)/2;

    // searchItem is a pointer array and the value I want to compare to is
    // at the index of counter (determined outside of this function)

    if((cond = strcmp(&charArray[position], &searchItem[counter])) == 0)
    {
        return position;
    }
    else if(cond < 0){
        begin = position + 1; 
    }

    else
        end = position - 1;
    }

return -1;
}

From here, going through the code by hand seems to make me want to think it should work fine, however it doesn't. I think I'm getting thrown off somewhere along the lines of my pointers and how I'm referring to them so the wrong data is being compared.

I've looked at it for too long now... really need some help here.

Upvotes: 0

Views: 2148

Answers (5)

Lundin
Lundin

Reputation: 213892

If these are ASCII strings and should be sorted in alphabetic order, I believe it should be

else if(cond < 0){
    end = position - 1; 
}

else
    begin = position + 1;
}

I'm not certain how you wish to sort them though?

Upvotes: 0

codaddict
codaddict

Reputation: 455112

It is not very clear what is being searched in what. But I'm guessing that you are searching for a character in a sorted character array. If that is the case, you can't use a strcmp. Instead you can do:

if(cond = (charArray[position] - *searchItem) == 0)

Upvotes: 2

MSalters
MSalters

Reputation: 179887

strcmp assumes that the strings being compared are zero-terminated, and exactly equal length. Therefore, strlen(&charArray[position]) has to equal strlen(&searchItem[counter]). That means position == strlen(&charArray[0]) - strlen(&searchItem[counter]). You don't need to search at all. Either the suffix of charArray matches or it doesn't.

But that's probably not what you intended. What are you trying to achieve?

Upvotes: 1

cli_hlt
cli_hlt

Reputation: 7164

strcmp compares all the characters in a char* up to the trailing '\0' character. So you cannot compare single characters (basically you always need two, the character and the trailing '\0') and you cannot compare parts of a string unless you insert a '\0' at the location up to which you want to perform the comparison.

Just for clarity, properly zero terminated strings (last character is '\0') are important for strcmp. strcmp compares two character arrays from the start up to the '\0' character and returns an appropriate comparison value (<0, =0, >0). And of course, both character arrays have to be the same length.

Upvotes: 0

Pete Wilson
Pete Wilson

Reputation: 8694

Are the strings to be compared all of the exact same length? Your code is assuming so. If not, you'll want to use strncmp( ), not strcmp().

Upvotes: 0

Related Questions