Usenet comp.sys.dec (1990-2013)
 help / color / mirror / Atom feed
* Qsort problem in DEC C
@ 1995-02-03 17:05 David Lloyd
  1995-02-03 18:59 ` David Jones
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: David Lloyd @ 1995-02-03 17:05 UTC (permalink / raw)


 All right, I've apparently gone brain-dead, but I cannot for the life of me
get the following C example to compile with DEC C 4.0 under OpenVMS/VAX V6.1.
 I get the warning message (between the dashed lines) that says (to me anyway)
"a" is not compatible with "a". Can anyone tell me where my programming and/or
linguistic skills left the room (with my sanity not far behind)? 

/* Program that does no useful work */

#include <stdlib.h>

int compare (int *a, int *b)
{
  if (a < b) return(-1);
  if (a == b) return(0);
  if (a > b) return(1);
}

void main(int argc, char** argv)
{
  
  int intarray[10] = {4,3,6,2,1,7,9,8,3,1};

  qsort(intarray, 10, sizeof(int), compare);

  return;
}

---------------------------------------------------------------------------

Line  305:        qsort(intarray, 10, sizeof(int), compare);
%CC-W-PTRMISMATCH, In this statement, the referenced type of the pointer value
 "compare" is "function (...) returning int", which is not compatible with
 "function (...) returning int".

---------------------------------------------------------------------------


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Qsort problem in DEC C
  1995-02-03 17:05 Qsort problem in DEC C David Lloyd
@ 1995-02-03 18:59 ` David Jones
  1995-02-03 19:33 ` John Buehler
  1995-02-06 23:14 ` Alan Rollow - Dr. File System's Home for Wayward Inodes.
  2 siblings, 0 replies; 4+ messages in thread
From: David Jones @ 1995-02-03 18:59 UTC (permalink / raw)


In article <lloyd-dr-0302950907550001@149.252.239.58>,
  lloyd-dr@nlvvax.llnl.egg.nv.doe.gov (David Lloyd) writes:
> All right, I've apparently gone brain-dead, but I cannot for the life of me
>get the following C example to compile with DEC C 4.0 under OpenVMS/VAX V6.1.
> I get the warning message (between the dashed lines) that says (to me anyway)
>"a" is not compatible with "a". Can anyone tell me where my programming and/or
>linguistic skills left the room (with my sanity not far behind)?
>
>/* Program that does no useful work */
>
>#include <stdlib.h>
>
>int compare (int *a, int *b)
>{
>  if (a < b) return(-1);
>  if (a == b) return(0);
>  if (a > b) return(1);
>}
>
>void main(int argc, char** argv)
>{
>
>  int intarray[10] = {4,3,6,2,1,7,9,8,3,1};
>
>  qsort(intarray, 10, sizeof(int), compare);
>
>  return;
>}


The stdlib header file defines the last argument to qsort as a pointer
to a funtion that take arguments (const void *, const void *).  The
compiler complains because you are passing a function with arguments
(int *, int *).

One workaround is to add the declaration "int compare();" to your main()
function, the old fashioned function declaration disables the type checking
on the arguments.

BTW, the compare() routine in your sample is going to compare the addresses
of the data items, not the values.


David L. Jones               |      Phone:    (614) 292-6929
Ohio State Unviversity       |      Internet:
2070 Neil Ave. Rm. 122       |               jonesd@kcgl1.eng.ohio-state.edu
Columbus, OH 43210           |               vman+@osu.edu

Disclaimer: Dogs can't tell it's not bacon.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Qsort problem in DEC C
  1995-02-03 17:05 Qsort problem in DEC C David Lloyd
  1995-02-03 18:59 ` David Jones
@ 1995-02-03 19:33 ` John Buehler
  1995-02-06 23:14 ` Alan Rollow - Dr. File System's Home for Wayward Inodes.
  2 siblings, 0 replies; 4+ messages in thread
From: John Buehler @ 1995-02-03 19:33 UTC (permalink / raw)


David Lloyd writes:

>int compare (int *a, int *b)

[snip]

>  qsort(intarray, 10, sizeof(int), compare);

>Line  305:        qsort(intarray, 10, sizeof(int), compare);
>%CC-W-PTRMISMATCH, In this statement, the referenced type of the pointer value
> "compare" is "function (...) returning int", which is not compatible with
> "function (...) returning int".

  The warning is complaining about the parameter types on your
function.  A qsort callback should take 'const void *' according to
the function prototype in stdlib.h

  Apparently, you have to declare your function with those types on
your parameters and then cast them to 'int *'.  If anybody else has a
better workaround, I'd be interested in hearing it.

JB
-- 
John Buehler
Template Software 
13100 Worldgate Drive, Suite 340       
Herndon, VA 22070-4382
email:  buehler@template.com    voice: 703-318-1000	fax:703-318-7378

"Inertia is nature's default"


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Qsort problem in DEC C
  1995-02-03 17:05 Qsort problem in DEC C David Lloyd
  1995-02-03 18:59 ` David Jones
  1995-02-03 19:33 ` John Buehler
@ 1995-02-06 23:14 ` Alan Rollow - Dr. File System's Home for Wayward Inodes.
  2 siblings, 0 replies; 4+ messages in thread
From: Alan Rollow - Dr. File System's Home for Wayward Inodes. @ 1995-02-06 23:14 UTC (permalink / raw)



In article <lloyd-dr-0302950907550001@149.252.239.58>, lloyd-dr@nlvvax.llnl.egg.nv.doe.gov (David Lloyd) writes:

> [ A common qsort compare routine problem. ]
>
>int compare (int *a, int *b)
>{
>  if (a < b) return(-1);
>  if (a == b) return(0);
>  if (a > b) return(1);
>}
>

Ignoreing the strange message about the arguments, they really
are pointers being passed.  Your code is comparing the value
of the pointers, not the integers they to which they point.

Useful code would look like:

	if( *a < *b )
		return -1 ;
	else if( *a == *b )
		return 0 ;
	else
		return 1 ;
--
Alan Rollow				alan@nabeth.cxo.dec.com



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~1995-02-06 23:14 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1995-02-03 17:05 Qsort problem in DEC C David Lloyd
1995-02-03 18:59 ` David Jones
1995-02-03 19:33 ` John Buehler
1995-02-06 23:14 ` Alan Rollow - Dr. File System's Home for Wayward Inodes.

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox