Define Prototype for a function that returns a pointer

Just starting out? Need help? Post your questions and find answers here.
StackC
New User
New User
Posts: 8
Joined: Sat Jun 17, 2023 6:35 pm

Define Prototype for a function that returns a pointer

Post 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?
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Define Prototype for a function that returns a pointer

Post 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.
"Have you tried turning it off and on again ?"
StackC
New User
New User
Posts: 8
Joined: Sat Jun 17, 2023 6:35 pm

Re: Define Prototype for a function that returns a pointer

Post 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?
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Define Prototype for a function that returns a pointer

Post 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.
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 ?"
StackC
New User
New User
Posts: 8
Joined: Sat Jun 17, 2023 6:35 pm

Re: Define Prototype for a function that returns a pointer

Post 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...
Last edited by StackC on Sun Jun 18, 2023 12:01 pm, edited 1 time in total.
infratec
Always Here
Always Here
Posts: 7625
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Define Prototype for a function that returns a pointer

Post 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()
Olli
Addict
Addict
Posts: 1244
Joined: Wed May 27, 2020 12:26 pm

Re: Define Prototype for a function that returns a pointer

Post 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.
Post Reply