Page 1 of 1

Define Prototype for a function that returns a pointer

Posted: Sun Jun 18, 2023 10:43 am
by StackC
Hello folks,

I am currently writing an include file for a DLL developed C and there are several functions which return a Pointer. How can I define a Prototype for those?

Re: Define Prototype for a function that returns a pointer

Posted: Sun Jun 18, 2023 11:27 am
by luis
If foo returns a pointer and has 3 int params just do

PrototypeC.i foo (a, b, c)

The returned int is your pointer.
Then retrieve the data from the pointer depending on the type as required (using typed struct variables for the returned value or Peek() functions or whatever).

If the pointer is being returned through one of the params just do

PrototypeC.i foo (a, b, *c)

And when you call the function do

foo (a, b, @*c)

so the C function has the address of the var *c and it can store the value of the pointer in it.

Then you use the pointer as *c

Anyway all this must be modified depending on the function prototype and the type of data.

Also see "pseudotypes" in the manual.

Re: Define Prototype for a function that returns a pointer

Posted: Sun Jun 18, 2023 11:30 am
by StackC
Hello luis,

thanks for your post. But it seems like an odd language design that we use .i for a pointer....I do not know any compiled language supporting pointers which is designed like that. Why can't we explicitly define a pointer as the return value?

Re: Define Prototype for a function that returns a pointer

Posted: Sun Jun 18, 2023 11:39 am
by luis
It is designed this way, PB tends to work with the basic data types.
Sometimes this is beneficial in my view because in the end everything it's a number and you can coerce it as you please without worrying about warning or continuously cast from type to type.
For example I hate all the tons of names used in the win32 API for the same thing (an int).
They are there for a reason, but I prefer the PB way.
With that said, I still would like to have a simple cast mechanism in PB because sometimes I miss it.

Re: Define Prototype for a function that returns a pointer

Posted: Sun Jun 18, 2023 11:41 am
by StackC
I see your point. But on the other hand, it really decreases maintainability. Because as a consumer of the API, I expect really an integer when I see .i, not a pointer...

Re: Define Prototype for a function that returns a pointer

Posted: Sun Jun 18, 2023 11:50 am
by infratec
What is a 'pointer' :?:

It is an address stored as what? As an integer.

If you want you can do

Code: Select all

Macro pointer
  i
EndMacro
Then you can use

Code: Select all

Procedure.pointer Test()

Re: Define Prototype for a function that returns a pointer

Posted: Sun Jun 18, 2023 1:16 pm
by Olli
StackC wrote:Why can't we explicitly define a pointer as the return value?
A pointor will ever have the same size as an integer.

Code: Select all

Structure pointor
 *pointor
EndStructure

Debug SizeOf(pointor)
Debug SizeOf(integer)
This code will respond

Code: Select all

4
4
or

Code: Select all

8
8
depending if you are executing your task on 32 bits or on 64 bits.

So, technically, a pointor is exactly the same variable as an integer.

A pointor in pureBasic is just there in a source code to ease the read of the coder.

As a pointor is syntaxically characterized by a '*' prefix, this would cause a strange design :

Code: Select all

Procedure.i myFunction()
...

*Procedure myFunction() ; FAKE LINE
...

OR

Declare.i myFunction()
*Declare myFunction() ; FAKE LINE

OR

Prototype.i myFunction()
*Prototype myFunction() ; FAKE LINE
So, in the three syntaxes above, no '*' character is required : this even is forbidden.

There only are these three ways, where this is like that, excepted a fourth detail below :

Code: Select all

Structure myStruc
*address
EndStructure

Define *myUFO.myStruc = AllocateMemory(8)
*myUFO\address = *anotherPointorValue

; not this fake line below :
*myUFO\*address = *anotherPointorValue
We get used to these four syntax exceptions very quickly.