473,386 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

bsearch() with dynamic array

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
4 2255
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ramprasad A Padmanabhan | last post by:
I have written a simple script to search a word in an array But bsearch does not seem to work here. I know I am missing out something very simple , But I am not able to find out what Thanks...
11
by: Ramprasad A Padmanabhan | last post by:
I have got a pretty simple script , that uses bsearch to look for a particular element The problem is , it simply segfaults inside the compare function. I have a similar script that works fine...
4
by: Angus Comber | last post by:
Hello I have received a lot of help on my little project here. Many thanks. I have a struct with a string and a long member. I have worked out how to qsort the struct on both members. I can...
6
by: Michiel Rapati-Kekkonen | last post by:
bsearch finds me only the first occurrence of something I'm looking for, but I would like to know the place in the list where it is found. The index of it's place in the array. So that I can check...
2
by: Michiel Rapati-Kekkonen | last post by:
recently I was put on the right trail in the matter of searching in arrays of structs. I got it working, a bit. Unfortunately, as soon as I want more, I'm stuck again: it is basically a...
4
by: Davy | last post by:
For example, I have a vector: double vector={1.11,2.38,4,53,17.14...,89.12,91.34} And if the Key I want is 5.2, the nearest item will be 4,53. I found that if the STEP of the vector is constant,...
2
by: Bit Byter | last post by:
I am hacking some legacy code and have put together a simple test to test some hashing funcs I've written. I now want to do a simplistic timing between the various structs. Here's a snippet: ...
0
by: jyotsnamk | last post by:
Hi, I am facing a problem using bsearch . Even though the data Im searching for is present in the array, the function returns a NULL object. The array is a structure with the following...
4
by: Amandil | last post by:
Hi, all. I'd like to check whether a certain string (one that I got from a user, or read from a file) is contained in a table of strings. The format of the table is char *table = { "string1",...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.