LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   qsort in C questions (https://www.linuxquestions.org/questions/programming-9/qsort-in-c-questions-331878/)

trutnev 06-09-2005 09:59 AM

qsort in C question
 
Hello,

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

osvaldomarques 06-09-2005 02:23 PM

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.

trutnev 06-09-2005 02:54 PM

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;
} NODE;

...

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

mohit dhawan 06-10-2005 05:40 AM

could u post ur compare function

trutnev 06-10-2005 08:49 AM

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


All times are GMT -5. The time now is 07:16 AM.