[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


Qsort problems...

Posted on 1999-01-13
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 200 total points
ID: 1255851
My answer: my previous comment.

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

656 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