简体   繁体   中英

Binary search with pointers in C

I have been asked to write a binary search program with some specific conditions. I have to pass the lower bound, upper bound, pointer to the array, and the search element. the program I have written gave me warnings. I am not able to correct the error in the program. Please point me where I am wrong.

#include <stdio.h>
int BinarySearch(int , int , int *, int );

int main()
{
    int n, i, a[20], h, l, x, r=0;
    int *p;

    printf("Enter the number of elements:\n");
    scanf("%d",&n);

    printf("Enter the elements:\n");
    for( i=0 ; i<n ; i++)
    {
        scanf("%d",&a[i]);
    }

    p = &a[0];
    printf("Enter the element to be searched:\n");
    scanf("%d", &x);

    l = 0;
    h = n-1;

    r = BinarySearch(l, h, p, x);

    if(r == 1)
    printf("The element %d is found in position %d", x, i);
    else
    printf("The element %d is not present in the array", x);

    return 0;
}

int BinarySearch(int l, int h, int *p, int x)
{
    int mid, a[20], f =0;
    *p = a[0];
    mid = ( l + h )/2;

    while( l <= h )
    {
        if( a[mid] == x )
        {
            f=1;
            break;
        }
        else if( a[mid] > x )
        {
            h = mid-1;
        }
        else if( a[mid] < x )
        {
            l = mid+1;
        }
    }
    if(f == 1)
    {return 1;}
    else
    {return -1;}
}

I get this warning when I compile this.

main.c|38|warning: 'a[0]' is used uninitialized in this function [-Wuninitialized]|

when I remove the line *p = a[0]; , then I get the following error messages:

main.c|43|warning: 'a[mid]' may be used uninitialized in this function [-Wmaybe-uninitialized]|
main.c|48|warning: 'a[mid]' may be used uninitialized in this function [-Wmaybe-uninitialized]|
main.c|52|warning: 'a[mid]' may be used uninitialized in this function [-Wmaybe-uninitialized]|

when I run the program, the program takes the value of the search element and terminates after some time. the control is not transferred to the binary search function.

Let's think how your int BinarySearch(int l, int h, int *p, int x) should work:

  1. l,h are the bounds
  2. p is pointer to data (the data is a[20] declared in main: int n, i, a[20], h, l, x, r=0; )
  3. x is the value to check against

If p is the pointer to your data ,there is no need (and it's actually wrong, because it will not search the data you want) in declaring a new a[20] inside the function, nor in *p = a[0]; , since p already points to the right address... Remove those, and use if( p[mid] == x ) instead of if( a[mid] == x )

The original array is defined inside main() and a separate array is defined in BinarySearch(). The pointer p is assigned to point to array defined in main.

p = &a[0];

The pointer p is passed in BinarySearch() and then the assignment

*p = a[0]

is infact changing the pointer contents to first element of new array defined in BinarySearch(). The new array is uninitialized and hence the appropriate compiler warnings.

Both previous answers seem valid, but the quick correction to the problem would be to revise:

int mid, a[20], f =0;
*p = a[0];

to

int mid, f =0;

Then update all references to a in function BinarySearch to p.

There is no need to allocate an entire array a when you are simply reading data from the array pointed to by p.

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