Preliminary Update to FBeginner EBook

New to FreeBASIC? Post your questions here.
robert
Posts: 169
Joined: Aug 06, 2019 18:45

Re: Preliminary Update to FBeginner EBook

Post by robert »

rdc wrote: All the code examples in the zip file folders have been fixed to run correctly,
qsort.bas, in FBeginner\chap09, does not compile cleanly.

fbc -w all "qsort.bas" (in directory: C:\dev\FB\FBeginner\chap09)
qsort.bas(31) warning 3(2): Passing different pointer types, at parameter 4 of QSORT()
qsort.bas(31) warning 41(0): Return type mismatch in function pointer, at parameter 4 of QSORT()
Compilation finished successfully.
rdc
Posts: 1741
Joined: May 27, 2005 17:22
Location: Texas, USA
Contact:

Re: Preliminary Update to FBeginner EBook

Post by rdc »

Those are just warnings but I will look at it. Thank you.
rdc
Posts: 1741
Joined: May 27, 2005 17:22
Location: Texas, USA
Contact:

Re: Preliminary Update to FBeginner EBook

Post by rdc »

I just compiled qsort.bas and I did not get any warnings or errors. It looks fine to me and runs correctly.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Preliminary Update to FBeginner EBook

Post by MrSwiss »

rdc wrote:I just compiled qsort.bas and I did not get any warnings or errors. It looks fine to me and runs correctly.
Have you tested with both compilers 32/64 bits?
It is important nowadays that the code is 'portable' (aka: runs w/o change with both compilers).
Since the user can use any one or even both 'bitness' (32/64).

Using U/Integer is a typical case to cause 'importable' code.
Usually replacing it with U/Long solves the problem (mostly, not always).
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Preliminary Update to FBeginner EBook

Post by fxm »

The passed function pointer must be of type:
FUNCTION CDECL(AS ANY PTR, AS ANY PTR) AS LONG
robert
Posts: 169
Joined: Aug 06, 2019 18:45

Re: Preliminary Update to FBeginner EBook

Post by robert »

fxm wrote:The passed function pointer must be of type:
FUNCTION CDECL(AS ANY PTR, AS ANY PTR) AS LONG
To elaborate on fxm's statement, specifically, in the case of qsort.bas, because the prototype for the qsort function contained in crt/stdlib.bi is

Code: Select all


Declare Sub qsort (ByVal As Any Ptr, ByVal As size_t, ByVal As size_t, ByVal As Function(ByVal As Any Ptr, ByVal As Any Ptr) As Long)

the QCompare function prototype must be declared as

Code: Select all


Declare Function QCompare cdecl (ByVal e1 As Any Ptr, ByVal e2 As Any Ptr) As Long

and the QCompare function defined as

Code: Select all


Function QCompare cdecl (ByVal e1 As Any Ptr, ByVal e2 As Any Ptr) As Long

The QSort function is then called by passing the address of the callback function.

Code: Select all


#include "crt/stdlib.bi"

qsort @myArray(0), 10, SizeOf(Integer), @QCompare

The above code snippets are taken from the FreeBASIC Documentation "The Pointer Data Type" section.
rdc
Posts: 1741
Joined: May 27, 2005 17:22
Location: Texas, USA
Contact:

Re: Preliminary Update to FBeginner EBook

Post by rdc »

Thanks for the info. I will update the program and ebook.
wallyg
Posts: 267
Joined: May 08, 2009 7:08
Location: Tucson Arizona

Re: Preliminary Update to FBeginner EBook

Post by wallyg »

Tried to download the book specified in the first post. It was not there. Is it still posted somewhere?

Wally
Post Reply