| | |
how do i sort reords from Z-A? (in turbo C)
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
look for qsort() -- all you have to do is write your own custom comparison function that returns an integer similar to strcmp(). The code below assums you are sorting an int array in descending order.
Or an array of strings, in descending order (I think)
C++ Syntax (Toggle Plain Text)
int mycomp(const void* p1, const void* p2) { int* i1 = (int*)p1; int* i2 = (int *)p2; return *i2 - *i1; }
Or an array of strings, in descending order (I think)
C++ Syntax (Toggle Plain Text)
int mycomp(const void* p1, const void* p2) { int x = strcmp((char*)p1,(char*)p2); return (x < 0) ? 1 : (x > 0) ? -1 : 0; }
AD: There's no need to cast away constness (it may do no harm, but I avoid things that can be bad habits). And your integer comparison function can be a victim of integer overflow.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
>return *i2 - *i1;
Eew. Can we say integer overflow?
>return (x < 0) ? 1 : (x > 0) ? -1 : 0;
Eeeeew! Nested conditional statements are fugly and hard to follow. How about this:
Eew. Can we say integer overflow?
>return (x < 0) ? 1 : (x > 0) ? -1 : 0;
Eeeeew! Nested conditional statements are fugly and hard to follow. How about this:
C++ Syntax (Toggle Plain Text)
int compare ( const void *a, const void *b ) { return -strcmp ( (const char *)a, (const char *)b ); }
I'm here to prove you wrong.
now how can subtracting two integers possibly cause integer overflow?
Narue: I agree your solution is simpler. But don't thumb you nose at nested conditional statements just because you may not understand them. Eeeeew :lol:
Dave: I'm sertain you already know that the two parameters must be cast to the correct type. So I don't know what you meant in your post. I could have posted this, but others, such as Narue, may not have understood it
Narue: I agree your solution is simpler. But don't thumb you nose at nested conditional statements just because you may not understand them. Eeeeew :lol:
Dave: I'm sertain you already know that the two parameters must be cast to the correct type. So I don't know what you meant in your post. I could have posted this, but others, such as Narue, may not have understood it
C++ Syntax (Toggle Plain Text)
return *(int *)p2 - *(int *)p1;
•
•
•
•
Originally Posted by Ancient Dragon
now how can subtracting two integers possibly cause integer overflow?
•
•
•
•
Originally Posted by Ancient Dragon
Dave: I'm sertain you already know that the two parameters must be cast to the correct type.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
>now how can subtracting two integers possibly cause integer overflow?
Under is over and over is under.
The point is that you could subtract beyond the limits of a signed integer, and Bad Things(TM) happen when you do stuff like that.
>But don't thumb you nose at nested conditional statements just
>because you may not understand them.
If everyone thumbed their noses at nested conditional statements, I wouldn't need to understand them. And the world would be a better place.
>I'm sertain you already know that the two parameters must be cast to the correct type.
And const is there for a reason. But for assignment there's no need to use casts:
When you go directly into a function call, a cast is a good idea, but remember that strcmp expects pointers to const char, not pointers to char. You don't cast to the actual type, you cast to the expected type and hope with all your might that the two types are compatible.
Under is over and over is under.
The point is that you could subtract beyond the limits of a signed integer, and Bad Things(TM) happen when you do stuff like that.>But don't thumb you nose at nested conditional statements just
>because you may not understand them.
If everyone thumbed their noses at nested conditional statements, I wouldn't need to understand them. And the world would be a better place.

>I'm sertain you already know that the two parameters must be cast to the correct type.
And const is there for a reason. But for assignment there's no need to use casts:
C++ Syntax (Toggle Plain Text)
int compare ( const void *a, const void *b ) { const int *ia = a; const int *ib = b; if ( *ia < *ib ) return +1; else if ( *ia > *ib ) return -1; else return 0; }
I'm here to prove you wrong.
•
•
•
•
Originally Posted by Dave Sinkula
What's INT_MAX - INT_MIN?
•
•
•
•
Originally Posted by Dave Sinkula
Right, you are incorrectly casting away the constness. Casting with (const int*) would be better.
•
•
•
•
Originally Posted by Narue
but remember that strcmp expects pointers to const char, not pointers to char.
C++ Syntax (Toggle Plain Text)
char name1[20]; char name2[20]; ... if( strcmp( (const char*)name1, (const char*)name2)) ...
I never explicitly typecast the parameters to const char*. So why bother in that comparison function I posted ?
And the same thing applies to all functions that take a const something-or-another.
•
•
•
•
Originally Posted by Ancient Dragon
-1 with my compiler
•
•
•
•
Originally Posted by Ancient Dragon
but you'r right about int overvlow in that one and only case.
And you could also start on the other side: Subtract from INT_MIN and positive number. Subtract from INT_MIN+1 every number greater than 2.
I'd say there are quite a few combinations of two integers that result in undefined behavior.
[edit]By the way, this is a comparison function I'd run across.
C++ Syntax (Toggle Plain Text)
int bar(const void *a, const void *b) { const int *x = a, *y = b; return (*x > *y) - (*x < *y); }
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
![]() |
Other Threads in the C++ Forum
- Previous Thread: Porting from pc to MAC
- Next Thread: checking for a space bar input
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets






