[英]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:
我在Pastebin上包含了相关文件:
#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);
}
#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 );
}
}
#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 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
我没有检查整个代码,但在
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
你的
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]);
}
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 尝试对代码进行划分,找出造成问题的部分,并以一小部分代码获得收益,这将有助于找出解决方案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.