Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 06-09-2005, 09:59 AM   #1
Registered: Sep 2003
Distribution: RedHat 9.0, Mandrake 9.1
Posts: 68

Rep: Reputation: 15
qsort in C question


there is an array of some structure, list of strings being a part of the structure. with 'bsearch' i find some element of the array and i want to sort its list of strings, i.e.

structure {
char** list;
} toto;

toto *array, *pointer;
pointer = bsearch( somekey, array, array_size, sizeof( toto), compare);

qsort( pointer->list, list_size, sizeof( char*), strcmp);

it does not sort.

thank you for any help

Last edited by trutnev; 06-09-2005 at 10:25 AM.
Old 06-09-2005, 02:23 PM   #2
Registered: Jul 2004
Location: Rio de Janeiro - Brazil
Distribution: Conectiva 10 - Conectiva 8 - Slackware 9 - starting with LFS
Posts: 519

Rep: Reputation: 34
Hi trutnev,

Looking the code excerpts you posted it's not clear if you are using an array of structures or a structure of arrays. The qsort header specifies the first parameter as the base pointer of the array and you are passing to it a pointer to an element of the structure and, it's not guaranteed to be the first element as it is the result of the bsearch. The second parameter must be the size of the array to be sorted and your excerpt does not give a clue from where "list_size" came. The third parameter must be the size of the array to be sorted and you give the size of a char pointer. And the last parameter may use strcmp to compare two elements of the array but, it could be used directly only if the array to be sorted is a character array.

If you could post some more code for us to see what use you do with your pointers I could help you with your sort issue.
Old 06-09-2005, 02:54 PM   #3
Registered: Sep 2003
Distribution: RedHat 9.0, Mandrake 9.1
Posts: 68

Original Poster
Rep: Reputation: 15
i have an array of structures

the structure contains an array of char strings

what i want to do is to insert a new char string to the existing array of char strings of some element of the array of structures

to see either this new string is not in the array, i want to use bsearch

to use bsearch, i need the array sorted with qsort

here is more detailed code

typedef struct {
int identifier;
char** words;
int words_size;


char buffer[SIZE];
NODE *lattice, *pointer, element;
int lattice_size;

scanf( "%d", &element.identifier);
scanf( "%s", buffer);

if(( pointer = bsearch( &element, lattice, lattice_size, sizeof( NODE), compare)) != NULL) {

/* i want to add new char string to the array of strings of the existing element of the array of structures */

if( bsearch( buffer, pointer->words, pointer->words_size, sizeof( char*), strcmp) == NULL)

/* I insert here the word at the end of the 'words' */

qsort( pointer->words, pointer->words_size, sizeof( char*), strcmp);
else {

/* i create here a new element of the array of structures */


all the sizes and values are initialised-treated in the code that i don't show here
Old 06-10-2005, 05:40 AM   #4
mohit dhawan
Registered: Mar 2005
Posts: 92

Rep: Reputation: 15
could u post ur compare function
Old 06-10-2005, 08:49 AM   #5
Registered: Sep 2003
Distribution: RedHat 9.0, Mandrake 9.1
Posts: 68

Original Poster
Rep: Reputation: 15
the problem that I found here is that qsort and bsearch of C cannot deal with arrays of char strings of arbitrary lengths (it is not the case of sort function of C++)

in my code, i do the search manually


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
basic questions on hostname and domain name + related postfix questions Moebius Linux - Newbie 7 09-04-2007 11:50 AM
qsort() on linux producing the wrong sort Adelsm Programming 17 09-27-2005 01:58 PM
In C, using qsort for dynamically allocated array ntmsz Programming 7 08-23-2005 10:33 AM
Solaris - Questions! Questions! Questions! qs_tahmeed Solaris / OpenSolaris 2 07-16-2005 05:27 AM
window manager questions and/or theme questions t3gah Linux - Software 2 02-27-2005 04:16 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:27 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration