1

So im trying to do a binary search in an array of strings called conj_str the thing is to do that i have to sort it and to that im trying to use qsort the problem is that comparsion function isnt working and its not sorting anything.

Program:

#include<stdlib.h> 
#include<stdio.h>
#include <string.h>
#define MAX_CHARS 1024
#define MAX_SIZE 10000

int compare (const void *a, const void *b)
{
  const char *key = a;
  const char * const *arg = b;
  return strcmp(key, *arg);
}

int main()
{
 int i;
 char conj_str[MAX_SIZE][MAX_CHARS];
 size_t len = sizeof(conj_str)/sizeof(const char *);
 strcpy(conj_str[0],"fcb");
 strcpy(conj_str[1],"bvb");
 strcpy(conj_str[2],"slb");
 strcpy(conj_str[3],"fcp");
 strcpy(conj_str[4],"rma");
 qsort (conj_str, len, sizeof (const char *), compare);
 for (i = 0; i < 5; i++) {
 printf ("%d: %s\n", i, conj_str[i]);
 }
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Martim Correia
  • 483
  • 5
  • 16

2 Answers2

1

In this call

qsort (conj_str, len, sizeof (const char *), compare);

there is specified incorrectly the size of array element. There must be

qsort (conj_str, len, sizeof ( char[MAX_CHARS]), compare);

Also this statement

int j = (int*) bsearch("fcb",conj_str,len,sizeof(const char *),compare);

does not make sense.

And in the comparison function this declaration

  const char * const *arg = b;

also does not make sense.

The function can look like

int compare (const void *a, const void *b)
{
  const char *key = a;
  const char *arg = b;
  return strcmp(key, arg);
}

and substitute this statement

size_t len = sizeof(conj_str)/sizeof(const char *);

for

size_t len = 5;

Here is your program with minor changes.

#include<stdlib.h> 
#include<stdio.h>
#include <string.h>
#define MAX_CHARS 1024
#define MAX_SIZE 10000

int compare (const void *a, const void *b)
{
  const char *key = a;
  const char *arg = b;
  return strcmp(key, arg);
}

int main()
{
    int i;
    char conj_str[MAX_SIZE][MAX_CHARS];
    size_t len = 5;

 strcpy(conj_str[0],"fcb");
 strcpy(conj_str[1],"bvb");
 strcpy(conj_str[2],"slb");
 strcpy(conj_str[3],"fcp");
 strcpy(conj_str[4],"rma");
 qsort (conj_str, len, sizeof ( char[MAX_CHARS]), compare);
 for (i = 0; i < 5; i++) {
 printf ("%d: %s\n", i, conj_str[i]);
 }


 char ( *p )[MAX_CHARS] = bsearch("fcb",conj_str,len,sizeof(char[MAX_CHARS]),compare);

if ( p )
{
    printf( "Found at position %zu\n", ( size_t )(p - conj_str ) );
}
else
{
    puts( "Not found" );
}

}

Its output is

0: bvb
1: fcb
2: fcp
3: rma
4: slb
Found at position 1
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

here a working rewrite of your code:

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

#define MAX_CHARS       1024
#define MAX_SIZE        5

int compare(const void *a, const void *b)
{
        return strcmp(a, b);
}

int main(void)
{
        int i;

        char conj_str[MAX_SIZE][MAX_CHARS];
        size_t len = sizeof(conj_str) / sizeof(conj_str[0]);
        strcpy(conj_str[0], "fcb");
        strcpy(conj_str[1], "bvb");
        strcpy(conj_str[2], "slb");
        strcpy(conj_str[3], "fcp");
        strcpy(conj_str[4], "rma");

        qsort(conj_str, len, sizeof(conj_str[0]), compare);

        for (i = 0; i < len; i++) {
                printf ("%d: %s\n", i, conj_str[i]);
        }

        char *s = bsearch("fcb", conj_str, len, sizeof(conj_str[0]), compare);
        puts(s ? "found" : "not found");
}

first thing: every string in the array should be initialized (at least to empty string) in order for qsort() and bsearch() to work, that's why I put:

#define MAX_SIZE        5

(and also because 10000 * 1024 are too much for an array on the stack)

then the size argument of both qsort() and bsearch() was wrong, in this case should be:

sizeof(conj_str[0])

since it's the size of the elements contained in the array.

bsearch() returns a pointer to the element if founded, so char *. The last line should be:

puts(s ? "found" : "not found");

where s is the return pointer of bsearch().

MarcoLucidi
  • 2,007
  • 1
  • 5
  • 8
  • yeah because of 10000*1024 being too big i used this ulimit -s unlimited, which allowed me to work with 10000 – Martim Correia May 19 '20 at 17:53
  • ok, but why do you need space for 10000 strings if you use only 5? – MarcoLucidi May 19 '20 at 17:54
  • well i used 5 for an example but i want to store large amounts of data and i just put 10k so that there would be enough space to store the strings – Martim Correia May 19 '20 at 17:56
  • I suggest you to look into dynamic memory allocation and the function `malloc()` (and `free()`). Big arrays on the stack are not recommended. – MarcoLucidi May 19 '20 at 17:59
  • the thing is i already tried this with dynamic memory allocation but i get a buffer overflow error, i even asked how i could solve this and people tried to help me but nothing worked so thats why im using a large number like 10k – Martim Correia May 19 '20 at 18:31