简体   繁体   中英

Segmentation fault in Binary Search in C

I'm trying to write a program that scans an input file that contains the number of letters in the array, a sorted list of letters, the number of letters to search for, a list of letters to search for. It displays the search results in a format shown in the sample file.

I'm getting a segmentation fault error message at runtime with the code I have included below. Now before this post gets negative feedback for not including the right amount of code, I don't really know where the error is with this segmentation fault. I've included the relevant files here on 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!

I did not check the whole code but I see this error inyour main.c

your code

    /* 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]);
    }

is wrong. your array shoud be double pointer 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]);
    }

Try to divide your code and found out the part wich is the cause of the problem and back a gain with a small part of code this will help to find out the solution

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM