Qsort problems...

Posted on 1999-01-13
Last Modified: 2010-04-15
having problems with qsort. I use watcom c/c++

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int compare( const void *op1, const void *op2 )
    const char **p1 = (const char **) op1;
    const char **p2 = (const char **) op2;
    return( strcmp( *p1, *p2 ) );

void main()
    char CharVect[3][10] = {{'\0'}};


    qsort( CharVect, 3,
          sizeof(char), compare );
    printf( "%s %s %s\n",
            CharVect[0], CharVect[1], CharVect[2] );

does not sort...any suggestions ?
Question by:migue
  • 2
  • 2
LVL 16

Expert Comment

ID: 1255847
You don't need the compare function and the width is wrong. You should be able to do:


LVL 16

Expert Comment

ID: 1255848
Note: the 10 is sizeof(CharVect[0]). Also qsort is expecting the elements to be presented in a block of memory (as CharVect is) and so simply passes pointers to the compare function, not pointers to pointers (it "knows" where the elements are given the information about the base address and the width).

LVL 14

Expert Comment

ID: 1255849
1. wrong answer!
2. >>    qsort( CharVect, 3,   sizeof(char), compare ); <--error!
  must be:  qsort( CharVect, 3,   sizeof(char *), compare );


Author Comment

ID: 1255850
answer is simply incorrect. I would like to cancel this issue, I wrote a quick sort myself.
Alex might be right.
LVL 14

Accepted Solution

AlexVirochovsky earned 100 total points
ID: 1255851
My answer: my previous comment.

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Read file descriptor 3 in C 14 107
how to understand recursion 12 226
What technology should I use for communication Raspberry Pi 2b and LapTop java application 2 124
delete-remove 14 84
This tutorial is posted by Aaron Wojnowski, administrator at  To view more iPhone tutorials, visit This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode ( They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question