1

Is there anyway to do something like this?

int key=50;
int loop=5;
int array[10]={...};
int* Ptr=NULL;


qsort(array, 10, sizeof(int), compareints);


while(loop>0){
  Ptr=(int*)bsearch(&key,array,10,sizeof(int),compareints);

  if(Ptr!=NULL){
    printf("found %d", *Ptr);
  }else{
    printf("did not find %d", *Ptr);
  }
  key++;
  loop--;
}

Problem is key gets incremented, but bsearch still searches for the number 50. I'm guessing because the key argument in bsearch is a constant pointer. I know it works if all the keys are stored in an array prior to searching. However, this doesn't suit my application. Any help would be appreciated.

alk
  • 69,737
  • 10
  • 105
  • 255
H_squared
  • 1,251
  • 2
  • 15
  • 32
  • bsearch expects argument 1 to be a pointer. http://www.cplusplus.com/reference/cstdlib/bsearch/ if I pass it as an int, then I get a warning – H_squared Sep 09 '13 at 13:21
  • 2
    You should be able to search for any key on any given iteration of your loop, so you need to show why you think it is still searching for `50`...maybe you need to show what's in your array's initializer? Could it be that your `compareints()` function is misbehaving? Maybe you should show that, too? Your `did not find` print should be printing `key` not `*Ptr`. Both `printf()` format strings should end with `\n` for sanity's sake. – Jonathan Leffler Sep 09 '13 at 13:25
  • Yes. I just wrote a sample code pretty quick (and forgot the '\n'). I'm actually searching in a struct array (printf("%d",Ptr->value) never changes). I will check the compare function, as it is the only thing I still haven't checked. But qsort worked perfectly with the compare function. – H_squared Sep 09 '13 at 13:31
  • We can't debug the code you don't show! – Jonathan Leffler Sep 09 '13 at 13:36

1 Answers1

1

Transcribing comment — and adding demonstration code.

You should be able to search for any key on any given iteration of your loop, so you need to show why you think it is still searching for 50...maybe you need to show what's in your array's initializer? Could it be that your compareints() function is misbehaving? Maybe you should show that, too? Your 'did not find' print should be printing key not *Ptr. Both printf() format strings should end with \n for sanity's sake.

This code works — and doesn't significantly change the logic shown in your question:

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

static
int compareints(const void *v1, const void *v2)
{
    int i1 = *(int *)v1;
    int i2 = *(int *)v2;
    if (i1 < i2)
        return -1;
    else if (i1 > i2)
        return +1;
    else
        return 0;
}

int main(void)
{
    int key = 50;
    int loop = 5;
    int array[10] = { 57, 49, 50, 51, 53, 27, 60, 51, 19, 99 };
    int *ptr = NULL;

    for (int i = 0; i < 10; i++)
        printf("%3d", array[i]);
    putchar('\n');
    qsort(array, 10, sizeof(int), compareints);
    for (int i = 0; i < 10; i++)
        printf("%3d", array[i]);
    putchar('\n');

    while (loop > 0)
    {
        printf("seeking key %d: ", key);
        ptr = (int *)bsearch(&key, array, 10, sizeof(int), compareints);

        if (ptr != NULL)
            printf("found %d\n", *ptr);
        else
            printf("did not find %d\n", key);
        key++;
        loop--;
    }
    return 0;
}

Sample output:

 57 49 50 51 53 27 60 51 19 99
 19 27 49 50 51 51 53 57 60 99
seeking key 50: found 50
seeking key 51: found 51
seeking key 52: did not find 52
seeking key 53: found 53
seeking key 54: did not find 54
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Thanks Jonathan. Now I know it works. Sorry about the code, unfortunately I might get in trouble, if my boss finds out I pasted a code in here. – H_squared Sep 09 '13 at 13:40
  • You need to learn how to create an SSCCE ([Short, Self-Contained, Correct Example](http://sscce.org/)) which retains your problem and the essence of your code, without revealing what cannot be revealed. It's perfectly doable — you change the names of everything except `main()` and the library functions; you ruthlessly eliminate the inessential; you repeatedly compile and test to ensure you've not eliminated the problem; when you can't remove anything, you've got your SSCCE. This is basic bug-reporting technique; every bug report you file anywhere (internal or external) should be an SSCCE. – Jonathan Leffler Sep 09 '13 at 13:43
  • I just solved my problem. It was very simple. I accidently declared Ptr as static int* . It worked once I removed static. – H_squared Sep 09 '13 at 14:06
  • Well, I'm glad it resolved your problem. It is not clear that changing any of the variables in my code from automatic to static would change the behaviour of the code — and specifically, `ptr` (née `Ptr`) being static or non-static should have no effect on the code. However, since we can't see your code, there may be something unusual about it such that the static-ness does matter (but 'unusual' is the operative term). – Jonathan Leffler Sep 09 '13 at 14:10