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?
Define Prototype for a function that returns a pointer
Re: Define Prototype for a function that returns a pointer
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.
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.
"Have you tried turning it off and on again ?"
Re: Define Prototype for a function that returns a pointer
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?
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
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.
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.
Last edited by luis on Sun Jun 18, 2023 12:04 pm, edited 1 time in total.
"Have you tried turning it off and on again ?"
Re: Define Prototype for a function that returns a pointer
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...
Last edited by StackC on Sun Jun 18, 2023 12:01 pm, edited 1 time in total.
Re: Define Prototype for a function that returns a pointer
What is a 'pointer'
It is an address stored as what? As an integer.
If you want you can do
Then you can use

It is an address stored as what? As an integer.
If you want you can do
Code: Select all
Macro pointer
i
EndMacro
Code: Select all
Procedure.pointer Test()
Re: Define Prototype for a function that returns a pointer
A pointor will ever have the same size as an integer.StackC wrote:Why can't we explicitly define a pointer as the return value?
Code: Select all
Structure pointor
*pointor
EndStructure
Debug SizeOf(pointor)
Debug SizeOf(integer)
Code: Select all
4
4
Code: Select all
8
8
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
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