0

please, could you help me with qsort of struct of strings? I need to sort alphabeticaly words in dictionary. Problem is, that it throws me Segmentation fault... Here are my structs:

typedef struct {
    int length;
    char *data;
} Word;

typedef struct {
    int length;
    int index;
    Word *data;
} Dictionary;

Here is compare function:

int compare(const void *a, const void *b) 
{ 
    return strcmp (((Word *)a)->data, ((Word *)b)->data);
} 

And here is qsort implementation:

qsort(&dictionary, dictionary.index, sizeof (Word *), compare); 

Thank you very much for your help.

3
  • Explain the use of length and index in Dictionary. Commented Dec 14, 2014 at 20:55
  • length is max length of dictionary and index is actual length Commented Dec 14, 2014 at 20:56
  • this qsort parameter: sizeof (Word *) is not correct, you do not want the size of a pointer, you want the size of the word struct. I.E. sizeof (Word ). There also seems to be something wrong with the first couple of parameters to qsort, but I'm not sure just what the problem is. Commented Dec 15, 2014 at 3:47

2 Answers 2

1

You are trying to qsort your dictionary structure rather than the dictionaries data.

qsort(dictionary.data, dictionary.index, sizeof (Word *), compare); 

You should also double check to make sure dictionary.index is the length of the dictionary and not dictionary.length

Sign up to request clarification or add additional context in comments.

9 Comments

and you need a better compare function.
After I add dictionary.data, it still throws segmentation fault. How to improve my compare function? Thank you
Have you tried running it in a debugger to see what is causing the segmentation fault? It would also be nice if you could edit your question and include a minimal compilable example.
I'm in C newbie and I'm not using any debugger yet. Could you be more specific about compilable example?
There are a number of good cheat-sheets and tutorials (e.g. gdb Command Cheatsheet). If you are going to program in C it is critical that you make friends with gdb. Go ahead, pay the price of admission, and spend several hours learning gdb. It will save you thousands of hours in the long run...
|
0

You should sort your Word-list of your Dictionary and not your Dictionary. If length is the number of all elements in your Word-list, you should use:

qsort(dictionary.data, dictionary.length, sizeof (dictionary.data[0]), compare);

and, you are right, your compare function is wrong, eg.

int compare(const void *a, const void *b) 
{ 
  const Word *x = a, *y =b;
  return strcmp (x->data,y->data); /* if data always contains a C string */
} 

2 Comments

Thank you for your answer. But however I edit it, it still throws me segmentation fault
Sorry, you were right. You just were probably confused about index and length. After I rewrite length to index, it works. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.