Preliminary Update to FBeginner EBook

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

Re: Preliminary Update to FBeginner EBook

Postby robert » Sep 20, 2020 19:37

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: 1725
Joined: May 27, 2005 17:22
Location: Texas, USA
Contact:

Re: Preliminary Update to FBeginner EBook

Postby rdc » Sep 20, 2020 20:09

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

Re: Preliminary Update to FBeginner EBook

Postby rdc » Sep 20, 2020 20:21

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

Re: Preliminary Update to FBeginner EBook

Postby MrSwiss » Sep 20, 2020 20:45

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
Posts: 9946
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Preliminary Update to FBeginner EBook

Postby fxm » Sep 20, 2020 21:15

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

Re: Preliminary Update to FBeginner EBook

Postby robert » Sep 21, 2020 0:09

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: 1725
Joined: May 27, 2005 17:22
Location: Texas, USA
Contact:

Re: Preliminary Update to FBeginner EBook

Postby rdc » Sep 21, 2020 1:23

Thanks for the info. I will update the program and ebook.

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 3 guests