Topic: Pointers and a question.
Author: apj@titan.oit.umass.edu (ADAM P JENKINS)
Date: 8 Feb 1995 19:37:07 GMT Raw View
Pete Becker (pete@borland.com) wrote:
: In article <Pine.BSI.3.91.950125003055.25815A-100000@chiba.netxn.com>,
: lionelm@chiba.netxn.com says...
: >
: >I have a bit of a problem wonder why this DOES work. Or at least, is
: >there a better solution. The pointer manipulation in the compare
: >functions does work correctly, but is there a better way of using the
: >pointers in this program?
: >
: >#include <stdlib.h>
: >#include <string.h>
: >#include <stdio.h>
: >
: >int compareStrUp( const void *, const void * );
: >int compareStrDown( const void *a, const void *b );
: >
: >int main( void )
: >{
: > register int a;
: > char *carray[ ] = {
: > "Absolute",
: > "Zany",
: > "Perplexed",
: > "Incontravertible",
: > "Victorious"
: > };
: >
: > qsort( (void **)carray, 5, sizeof( char * ), compareStrUp );
: > for( a = 0; a < 5; a++ ) puts( carray[ a ] );
: >
: > qsort( (void **)carray, 5, sizeof( char * ), compareStrDown );
: > for( a = 0; a < 5; a++ ) puts( carray[ a ] );
: > return 0;
: >}
: >
: >int compareStrUp( const void *a, const void *b )
: >{ // This just
: > return( strcmp( *(char **)a, *(char **)b ) ); // doesn't look
: >} // right!
: >
: >int compareStrDown( const void *a, const void *b )
: >{
: > return( -strcmp( *(char **)a, *(char **)b ) ); // or this
: >}
I always just use character pointers if I know that that is what qsort
will be passing to the compare function. As in:
int comparStrUp( const char **a, const char **b)
{
return(strcmp(*a, *b));
}
That looks a little cleaner I think. But there's no problem with
the way you have it, and it's probably more "correct".
--------------------------------------------------------------
Adam P. Jenkins
apj@twain.oit.umass.edu
Author: nevets@psp.att.com ( )
Date: Thu, 9 Feb 1995 13:44:36 GMT Raw View
In article cvt@nic.umass.edu, apj@titan.oit.umass.edu (ADAM P JENKINS) writes:
> Pete Becker (pete@borland.com) wrote:
> : In article <Pine.BSI.3.91.950125003055.25815A-100000@chiba.netxn.com>,
> : lionelm@chiba.netxn.com says...
> : >
> : >I have a bit of a problem wonder why this DOES work. Or at least, is
> : >there a better solution. The pointer manipulation in the compare
> : >functions does work correctly, but is there a better way of using the
> : >pointers in this program?
> : >
> : >#include <stdlib.h>
> : >#include <string.h>
> : >#include <stdio.h>
> : >
> : >int compareStrUp( const void *, const void * );
> : >int compareStrDown( const void *a, const void *b );
> : >
> : >int main( void )
> : >{
> : > register int a;
> : > char *carray[ ] = {
> : > "Absolute",
> : > "Zany",
> : > "Perplexed",
> : > "Incontravertible",
> : > "Victorious"
> : > };
> : >
> : > qsort( (void **)carray, 5, sizeof( char * ), compareStrUp );
> : > for( a = 0; a < 5; a++ ) puts( carray[ a ] );
> : >
> : > qsort( (void **)carray, 5, sizeof( char * ), compareStrDown );
> : > for( a = 0; a < 5; a++ ) puts( carray[ a ] );
> : > return 0;
> : >}
> : >
> : >int compareStrUp( const void *a, const void *b )
> : >{ // This just
> : > return( strcmp( *(char **)a, *(char **)b ) ); // doesn't look
> : >} // right!
> : >
> : >int compareStrDown( const void *a, const void *b )
> : >{
> : > return( -strcmp( *(char **)a, *(char **)b ) ); // or this
> : >}
>
If you look at *(char **), this simply reduces to (char *) which
is what strcmp() expects.
Steve Ringwood
Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: Sat, 11 Feb 1995 04:10:56 GMT Raw View
nevets@psp.att.com ( ) writes:
>apj@titan.oit.umass.edu (ADAM P JENKINS) writes:
>> : >int compareStrUp( const void *a, const void *b )
>> : >{ // This just
>> : > return( strcmp( *(char **)a, *(char **)b ) ); // doesn't look
>> : >} // right!
>
> If you look at *(char **), this simply reduces to (char *) which
> is what strcmp() expects.
No, `*(char **)a' is a very different thing to `(char *)a'.
The former dereferences `a' but the latter does not.
--
Fergus Henderson - fjh@munta.cs.mu.oz.au
all [L] (programming_language(L), L \= "Mercury") => better("Mercury", L) ;-)
Author: chris.smith@lightspeed.com (Chris Smith)
Date: 11 Feb 95 05:00:00 GMT Raw View
In article <3hb6h3$cvt@nic.umass.edu>
apj@titan.oit.umass.edu (ADAM P JENKINS) wrote:
<some code elided>
APJ>: >int compareStrUp( const void *, const void * );
APJ>: >int compareStrDown( const void *a, const void *b );
APJ>: >
APJ>: >int main( void )
APJ>: >{
APJ>: > register int a;
APJ>: > char *carray[ ] = {
APJ>: > "Absolute",
APJ>: > "Zany",
APJ>: > "Perplexed",
APJ>: > "Incontravertible",
APJ>: > "Victorious"
APJ>: > };
APJ>: >
APJ>: > qsort( (void **)carray, 5, sizeof( char * ), compareStrUp );
APJ>I always just use character pointers if I know that that is what qsort
APJ>will be passing to the compare function. As in:
APJ>int comparStrUp( const char **a, const char **b)
APJ>{
APJ>return(strcmp(*a, *b));
APJ>}
APJ>That looks a little cleaner I think. But there's no problem with
APJ>the way you have it, and it's probably more "correct".
The way you have it may look prettier, but under ANSI C or C++, it
is not "correct". Pre-ANSI C compilers accepted your code, but if
your ANSI conformant C compiler or C++ compiler compiles it, the
type-checking is busted. qsort takes
int (*)(const void*, const void*)
as the fourth argument, NOT
int (*)(const char**, const char**)
and this is _not_ one of the conversions that the compiler is
allowed to make. Note, that I am not referring to converting a
const void* to a const char**, but converting
int (*)(const char**, const char**) to
int (*)(const void**, const void**)
You _could_ do this:
typedef int (*mycmp)(const char**, const char**);
int fcmp(const char**, const char**);
qsort((void **)carray, 5, sizeof(char*), (mycmp)compareStrUp);
But that's still not real pretty.
---
. OLX 2.2 . Are you into casual sex, or should I dress up?
Author: pete@borland.com (Pete Becker)
Date: Tue, 31 Jan 1995 01:46:05 GMT Raw View
In article <Pine.BSI.3.91.950125003055.25815A-100000@chiba.netxn.com>,
lionelm@chiba.netxn.com says...
>
>I have a bit of a problem wonder why this DOES work. Or at least, is
>there a better solution. The pointer manipulation in the compare
>functions does work correctly, but is there a better way of using the
>pointers in this program?
>
>#include <stdlib.h>
>#include <string.h>
>#include <stdio.h>
>
>int compareStrUp( const void *, const void * );
>int compareStrDown( const void *a, const void *b );
>
>int main( void )
>{
> register int a;
> char *carray[ ] = {
> "Absolute",
> "Zany",
> "Perplexed",
> "Incontravertible",
> "Victorious"
> };
>
> qsort( (void **)carray, 5, sizeof( char * ), compareStrUp );
> for( a = 0; a < 5; a++ ) puts( carray[ a ] );
>
> qsort( (void **)carray, 5, sizeof( char * ), compareStrDown );
> for( a = 0; a < 5; a++ ) puts( carray[ a ] );
> return 0;
>}
>
>int compareStrUp( const void *a, const void *b )
>{ // This just
> return( strcmp( *(char **)a, *(char **)b ) ); // doesn't look
>} // right!
>
>int compareStrDown( const void *a, const void *b )
>{
> return( -strcmp( *(char **)a, *(char **)b ) ); // or this
>}
>
>Any suggestions for cleaning it up?
>
>
It looks fine to me. But if you really don't like it, you might
consider using a typedef:
typedef char *Str;
-- Pete
Author: Lionel Mauritson <lionelm@chiba.netxn.com>
Date: Wed, 25 Jan 1995 00:35:54 -0800 Raw View
I have a bit of a problem wonder why this DOES work. Or at least, is
there a better solution. The pointer manipulation in the compare
functions does work correctly, but is there a better way of using the
pointers in this program?
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
int compareStrUp( const void *, const void * );
int compareStrDown( const void *a, const void *b );
int main( void )
{
register int a;
char *carray[ ] = {
"Absolute",
"Zany",
"Perplexed",
"Incontravertible",
"Victorious"
};
qsort( (void **)carray, 5, sizeof( char * ), compareStrUp );
for( a = 0; a < 5; a++ ) puts( carray[ a ] );
qsort( (void **)carray, 5, sizeof( char * ), compareStrDown );
for( a = 0; a < 5; a++ ) puts( carray[ a ] );
return 0;
}
int compareStrUp( const void *a, const void *b )
{ // This just
return( strcmp( *(char **)a, *(char **)b ) ); // doesn't look
} // right!
int compareStrDown( const void *a, const void *b )
{
return( -strcmp( *(char **)a, *(char **)b ) ); // or this
}
Any suggestions for cleaning it up?
Lionel J Mauritson Jr | In the long-run every Government is the
Bakersfield, CA 93307-4120 | exact symbol of its People, with their
Business # (805)837-4774 | wisdom and unwisdom.
lionelm@chiba.netxn.com | -Thomas Carlyle, "Past and Present"