473,804 Members | 4,005 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2278
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","ST RING_1", \
"STRING_2","STR ING_3","STRING_ 4","STRING_5 ", \
"STRING_6","STR ING_7","STRING_ 8","STRING_9 "}

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

int comparison(cons t void *, const void *);
void free_sMyArray(c har **sMyArray, size_t nmemb);

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

sMyArray = malloc(NMEMB(te n) * sizeof *sMyArray);
for (index = 0; index != NMEMB(ten); ++index) {
sMyArray[index] = malloc(sizeof "STRING_8") ;
if (sMyArray[index] == NULL) {
free_sMyArray(s MyArray, index);
puts("sMyArray[index] == NULL");
exit(EXIT_FAILU RE);
}
}
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("searchi ng 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(s MyArray, NMEMB(ten));
return 0;
}

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

void free_sMyArray(c har **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
4890
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 Ram
11
2240
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 , and now this simply segfaults. I am driving myself nuts , last 4 hrs , why such a simple thing would not work Pls someone help
4
5605
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 do a bsearch on the long member (nKey) but I am struggling to do a search using the string member. The code I am running appears below. It doesn't crash or anything. It is just that when I do the last bsearch using "192.168.1.3" I SHOULD find...
6
5413
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 if there are more to find on the next places. I need to find all occurences. Maybe anyone of you know even a better way? thanks, in advance! Michiel Rapati
2
2006
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 dictionary I'm trying to make. I have a huge list of conjugated forms. One can search that list to find a number, the index in another array, which refers to the unconjugated word.
4
3439
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, something like {1.1,1.2,1.3,1.4,...} the compare function will be int compare (const void * a, const void * b) {
2
3098
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: struct item_{ char key; char data; };
0
1509
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 definition struct ppcData { char MSISDN; char StatusInFile;
4
4071
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", "string2", "string3", ..., NULL } I wrote my own function that uses strcmp(): int table_lookup(char *s, char *list) { char *t;
0
9706
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10330
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10319
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10076
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6851
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4297
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.