By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,780 Members | 1,549 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,780 IT Pros & Developers. It's quick & easy.

bsearch() with dynamic array

P: n/a
Hello,

I have filled a dynamic array of strings (realloc() + malloc())

char **sMyArray;

At the end, I get correctly sMyArray[0] = "STRING_0", sMyArray[1] =
"STRING_1", etc...

But I'm unable to use bsearch(). It doens't find any string.

(and nSize = sizeof(sMyArray[0]) = 4, which is not what I would want (8))

Is it possible ?

Thanks in advance.

Jun 23 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Steph <nospamsaid:
Hello,

I have filled a dynamic array of strings (realloc() + malloc())

char **sMyArray;

At the end, I get correctly sMyArray[0] = "STRING_0", sMyArray[1] =
"STRING_1", etc...

But I'm unable to use bsearch(). It doens't find any string.

(and nSize = sizeof(sMyArray[0]) = 4, which is not what I would want
(8))
It might not be what you want, but it's what you get when you use sizeof
to calculate the number of bytes occupied by a pointer (on your
platform, at least - the number may vary on other platforms).
>
Is it possible ?
It is certainly possible to use bsearch to find a string within a sorted
array of strings, yes. I conclude that there is a bug in your program.
Solution: find and fix the bug.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 23 '07 #2

P: n/a
On Sat, 23 Jun 2007 11:11:07 +0000, Richard Heathfield wrote:
>Steph said:
>(and nSize = sizeof(sMyArray[0]) = 4, which is not what I would want
(8))

It might not be what you want, but it's what you get when you use sizeof
to calculate the number of bytes occupied by a pointer
See http://c-faq.com/malloc/sizeof.html
--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
Jun 23 '07 #3

P: n/a
Steph wrote:
Hello,

I have filled a dynamic array of strings (realloc() + malloc())

char **sMyArray;

At the end, I get correctly sMyArray[0] = "STRING_0", sMyArray[1] =
"STRING_1", etc...

But I'm unable to use bsearch(). It doens't find any string.

(and nSize = sizeof(sMyArray[0]) = 4, which is not what I would want (8))

Is it possible ?
My guess is that you are confused about the nature
of your dynamic array. You have not created an array
of four eight-char strings, but an array of four char*
pointers that in turn point to the beginnings of the
strings. The comparison function therefore receives
two void* pointers that point to two char* pointers
that point to the characters, and you must handle this
arrangement correctly in your comparison function.

By the way, "STRING_0" is eight characters long but
requires *nine* characters of storage. If you're trying
to cram these nine-character objects into eight-character
slots, that's yet another source of trouble.

--
Eric Sosman
es*****@acm-dot-org.invalid
Jun 23 '07 #4

P: n/a
Steph wrote:
>
Hello,

I have filled a dynamic array of strings (realloc() + malloc())

char **sMyArray;

At the end, I get correctly sMyArray[0] = "STRING_0", sMyArray[1] =
"STRING_1", etc...

But I'm unable to use bsearch(). It doens't find any string.

(and nSize = sizeof(sMyArray[0]) = 4,
which is not what I would want (8))

Is it possible ?
/* BEGIN new.c */

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

#define KEY "STRING_8"
#define TEN {"STRING_0","STRING_1", \
"STRING_2","STRING_3","STRING_4","STRING_5", \
"STRING_6","STRING_7","STRING_8","STRING_9"}

#define NMEMB(A) (sizeof (A) / sizeof (*A))

int comparison(const void *, const void *);
void free_sMyArray(char **sMyArray, size_t nmemb);

int main(void)
{
char *ten[] = TEN;
char *key = KEY;
char **sMyArray;
char **found;
size_t index;

sMyArray = malloc(NMEMB(ten) * sizeof *sMyArray);
for (index = 0; index != NMEMB(ten); ++index) {
sMyArray[index] = malloc(sizeof "STRING_8");
if (sMyArray[index] == NULL) {
free_sMyArray(sMyArray, index);
puts("sMyArray[index] == NULL");
exit(EXIT_FAILURE);
}
}
qsort(ten, NMEMB(ten), sizeof *ten, comparison);
for (index = 0; index != NMEMB(ten); ++index) {
strcpy(sMyArray[index], ten[index]);
puts(sMyArray[index]);
}
putchar('\n');
printf("searching for %s\n", key);
found = bsearch
(&key, sMyArray, NMEMB(ten), sizeof *ten, comparison);
if (found == NULL) {
puts("found == NULL");
} else {
printf("found %s\n", *found);
}
free_sMyArray(sMyArray, NMEMB(ten));
return 0;
}

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

void free_sMyArray(char **sMyArray, size_t nmemb)
{
while (nmemb-- != 0) {
free(sMyArray[nmemb]);
}
free(sMyArray);
}

/* END new.c */

--
pete
Jul 8 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.