c - C中的二进制搜索中的段错误

标签 c binary-search

我正在尝试编写一个程序来扫描输入文件,该文件包含数组中的字母数、排序的字母列表、要搜索的字母数、要搜索的字母列表。它以示例文件中显示的格式显示搜索结果。

我在运行时使用下面包含的代码收到段错误消息。现在,在这篇文章因为没有包含正确数量的代码而得到负面反馈之前,我真的不知道这个段错误的错误在哪里。我已将相关文件包含在 Pastebin 中:

main.c

#include <stdio.h>
#include <stdlib.h>
#include "Proto.h"

int main()
{
    /* Accepts number of elements from user */
    scanf("%d", &elements);

    /* Creates dynamic array */
    array = (char *) calloc(elements, sizeof(char));

    /* Copies sorted values to the dynamic array */
    for(i = 0; i < elements; i++)
    {
        scanf("%s", &array[i]);
    }

    /* Accepts number of elements to search */
    scanf("%d", &search);

    /* Searches for elements in sorted array one at a time */
    for(i = 1; i <= search; i++)
    {
        /* Accepts value to search */
        scanf("%s", &value);

        /* Resets counter to 0 */
        count = 0;

        /* Finds location of element in the sorted list using binary search */
        location = binarySearch(array, value, 0, (elements-1));

        /* Checks if element is present in the sorted list */
        if (location == -1)
        {
            printf("%4s not found!\n", value);
        }

        else
        {
            printf("%4s found at %4d iteration during iteration %4d\n", value, location, count);
        }
    }
    free(array);
}

BinarySearch.c

#include <stdio.h>
#include "Proto.h"

int binarySearch(char * nums, char svalue, int start, int end)
{
    middle = (start + end) / 2;

    /* Target found */
    if (nums[middle] == svalue)
    {
        return middle;
    }

    /* Target not in list */
    else if( start == end )
    {
        return -1;
    }

    /* Search to the left */
    else if( nums[middle] > svalue )
    {
        count++;
        return binarySearch( nums, svalue, start, (middle-1) );
    }

    /* Search to the right */
    else if( nums[middle] < svalue )
    {
        count++;
        return binarySearch( nums, svalue, (middle+1), end );
    }
}

Proto.h

#ifndef _PROTO_H
#define _PROTO_H

char * array;
int elements, search, location, count, middle, i;
char value;
int binarySearch(char *, char, int, int);

#endif

Sample Input/Output

Sample Input file:
6
a d n o x y
3
n x z

Sample Output file:
  n found at    2 during iteration    0. 
  x found at    4 during iteration    1.
  z not found!

最佳答案

我没有检查整个代码,但我在你的 main.c 中看到了这个错误

你的代码

    /* Creates dynamic array */
    array = (char *) calloc(elements, sizeof(char));

    /* Copies sorted values to the dynamic array */
    for(i = 0; i < elements; i++)
    {
            scanf("%s", &array[i]);
    }

错了。你的 array 应该是双指针 char **array

    /* Creates dynamic array */
    array = calloc(elements, sizeof(char*));

    /* Copies sorted values to the dynamic array */
    for(i = 0; i < elements; i++)
    {
            scanf("%ms", &array[i]);
    }

尝试划分你的代码并找出导致问题的部分并用一小部分代码返回增益这将有助于找到解决方案

关于c - C中的二进制搜索中的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15906652/

相关文章:

c - 处理 fscanf()

c - 保存通用链表的哈希集 保存通用链表

Haskell 中外部 C 指针后的清理

c - 使用小批量时如何更新权重?

C++过滤不相关坐标数据的算法

c++ - 在平方二进制矩阵中找到二进制矩形的位置

java Arrays.binarySearch() 在不区分大小写的搜索中无法找到字符串

c - 将节点添加到指针数组

algorithm - 如何从逻辑上解释二进制搜索的任何变体

c# - 按 ID 的 BinarySearch 对象数组