使用“strcmp”关于 c 中字符数组的特定成员

发布于 2025-01-04 15:22:44 字数 808 浏览 0 评论 0原文

我有一个二分搜索函数,我传递一个指针字符数组、该数组的长度、一个搜索指针字符数组和另一个用于其他内容的计数器。

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

从这里开始,手动浏览代码似乎让我认为它应该可以正常工作,但事实并非如此。我认为我在我的指针以及我如何引用它们的地方被抛弃了,所以比较了错误的数据。

我已经看了太久了......这里真的需要一些帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

苏别ゝ 2025-01-11 15:22:44

目前还不清楚正在什么内容中进行搜索。但我猜测您正在排序的字符数组中搜索字符。如果是这种情况,则不能使用 strcmp。相反,你可以这样做:

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

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)
御弟哥哥 2025-01-11 15:22:44

strcmp 假定所比较的字符串以零结尾,并且长度完全相同。因此,strlen(&charArray[position]) 必须等于 strlen(&searchItem[counter])。这意味着 position == strlen(&charArray[0]) - strlen(&searchItem[counter])。您根本不需要搜索。 charArray 的后缀要么匹配,要么不匹配。

但这可能不是您想要的。你想达到什么目的?

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?

千と千尋 2025-01-11 15:22:44

要比较的字符串的长度是否完全相同?您的代码假设是这样。如果没有,您将需要使用 strncmp( ),而不是 strcmp()。

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

心的憧憬 2025-01-11 15:22:44

strcmp 比较 char* 中直到尾随“\0”字符的所有字符。因此,您无法比较单个字符(基本上您总是需要两个字符,即字符和尾随的“\0”),并且无法比较字符串的各个部分,除非您在要执行的位置插入“\0”比较。

为了清楚起见,正确的以零结尾的字符串(最后一个字符是“\0”)对于 strcmp 很重要。 strcmp 比较两个字符数组(从开头到“\0”字符)并返回适当的比较值(<0、=0、>0)。当然,两个字符数组的长度必须相同。

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.

眼睛会笑 2025-01-11 15:22:44

如果这些是 ASCII 字符串并且应该按字母顺序排序,我相信应该是

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

else
    begin = position + 1;
}

我不确定您希望如何对它们进行排序?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文