0

我正在使用 C 语言进行代码练习。

如下代码,

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define _CRT_SECURE_NO_WARNINGS

int ACDSort(const void *p1, const void *p2);
int Compare(const void *pKey, const void *pValue);
int main(void)
{
    char * strAry[4] = {"Hardware","Cookie","Boy","Power"};
    char * destStr = "Cookie";

    //qsort((void*)strAry, sizeof(strAry) / sizeof(char*), sizeof(char*), ACDSort);

    char **ptrAdr = (char**)bsearch((void*)destStr, strAry, sizeof(strAry) / sizeof(char*), sizeof(char*), Compare);

    printf("%s\n", *ptrAdr);
}

int Compare(const void *pKey, const void *pValue) {
    char *key = ((char*)pKey);
    char *value = *((char**)pValue);
    return strcmp(key, value);
}

int ACDSort(const void *p1, const void *p2) {

    char * n1 = *((char**)p1);
    char * n2 = *((char**)p2);
    int ret;

    if (strlen(n1) > strlen(n2))
        ret = 1;
    else if (strlen(n1) < strlen(n2))
        ret = -1;
    else
        ret = 0;
    return ret;
}

我打电话bsearch找字符串cookie//问题是当我删除以根据字符串长度对数组进行排序时发生错误。我不知道为什么会执行错误,因为我认为这qsort不会对我的代码产生重大影响。

你能告诉我在擦除时返回空指针错误的原因//吗?

附言。我用qsortandbsearch来熟悉指针变量。

4

2 回答 2

5

bsearch使用二进制搜索,这就是原因。二分查找需要对数据进行排序。按字母顺序对字符串数组进行排序,它将起作用。

作为旁注,您需要摆脱所有那些多余的演员表,他们所做的只是隐藏潜在的错误。

修复和清理后的工作程序:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int compare (const void* p1, const void* p2);

int main (void)
{
    const char* strAry[4] = {"Boy", "Cookie", "Hardware", "Power"};
    const char* key = "Cookie";

    char** ptrAdr = bsearch(key, 
                            strAry, 
                            sizeof(strAry)/sizeof(*strAry), 
                            sizeof(*strAry), 
                            compare);

    printf("%s\n", *ptrAdr);
}

int compare (const void* p1, const void* p2) 
{
  const char* s1 = p1;
  const char* s2 = *(const char* const*)p2;

  return strcmp(s1, s2);
}

p2 最终会得到一个指向 a 的constvoid 指针const char*,这就是为什么我们在努力获得 const 正确性时会得到看起来很奇怪的演员表。

于 2016-09-29T06:31:42.467 回答
-1

type ofdestStr可以更改为与 type of 相同strAry,例如:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int Compare(const void *pKey, const void *pValue)
{
    char *key = *((char**)pKey);
    char *value = *((char**)pValue);
    return strcmp(key, value);
}

int main(void)
{
    char * strAry[4] = { "Hardware", "Cookie", "Boy", "Power" };
    char * destStr[1] = { "Cookie" }; // Type changing

    qsort(strAry, sizeof(strAry) / sizeof(char*), sizeof(char*), Compare);

    char **ptrAdr = (char**)bsearch((void*)destStr, strAry, sizeof(strAry) / sizeof(char*), sizeof(char*), Compare);

    printf("%s\n", *ptrAdr);
}

此外,如果您使用 C 语言(和编译器),请考虑strcmp直接用作比较两个元素的回调函数。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*int Compare(const void *pKey, const void *pValue)
{
    char *key = *((char**)pKey);
    char *value = *((char**)pValue);
    return strcmp(key, value);
}*/

int main(void)
{
    char * strAry[4] = { "Hardware", "Cookie", "Boy", "Power" };
    char * destStr[1] = { "Cookie" }; // Type changing

    //qsort(strAry, sizeof(strAry) / sizeof(char*), sizeof(char*), Compare);
    qsort(strAry, sizeof(strAry) / sizeof(char*), sizeof(char*), strcmp);

    //char **ptrAdr = (char**)bsearch((void*)destStr, strAry, sizeof(strAry) / sizeof(char*), sizeof(char*), Compare);
    char **ptrAdr = (char**)bsearch(destStr, strAry, sizeof(strAry) / sizeof(char*), sizeof(char*), strcmp);

    printf("%s\n", *ptrAdr);
}

笔记:

该解决方案有一个缺点,表现为...

警告:从不兼容的指针类型传递“bsearch”的参数 5 [默认启用]

但它有效(我尝试过使用 GCC 版本 4.8.2 和 MS Visual Studio 12.0)

于 2016-09-29T07:40:09.077 回答