To point or not to point

Just starting out? Need help? Post your questions and find answers here.
mubinaktan
User
User
Posts: 19
Joined: Mon Oct 08, 2007 11:25 am

To point or not to point

Post by mubinaktan »

I am trying to learn using dll functions. I have seen an example from the "PureBASIC Survival Guide" (http://www.xs4all.nl/~bluez/datatalk/pu ... i_and_dlls) like this:

Code: Select all

OpenLibrary(1,"MyMagical.dll")
*a_pointer = GetFunction(1,"blahblahblah")
CallFunctionFast(*a_pointer,first_variable,second_variable) 
It works. Good...
As far as I understand, first we select a function from a dll and store its memory address in a pointer (do I understand right?)

But if we use a regular variable instead of a pointer, it works again!

Code: Select all

 non_pointer_variable = GetFunction(1,"blahblahblah")
CallFunctionFast(non_pointer_variable,first_variable,second_variable) 
How is this possible? To my logic, first example should work but the second one should not work. The pointers are for storing memory addresses so don't we HAVE TO use a pointer when storing the memory address of a function?
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

A pointer is just a variable that stores a memory address. But a memory address can also be stored in other variable types, as long as the type supports large enough numbers. The default type is long, which is a 4 byte integer, the same size as a pointer.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

In Purebasic, a pointer is just a variable of type long which we usually use to store addresses. Until you use pointers in combination with a structure, however, Purebasic does not differentiate between a pointer and a normal variable of type long.

For example, the following is a function for adding two values and returning the result. The fact that I use pointer varables is neither here or there :

Code: Select all

Procedure.l Add(*a, *b)
  ProcedureReturn *a + *b
EndProcedure

Debug Add(10, 20)
Don't confuse PB pointers with those from, say, c, in which they are strictly typed from the outset. In PB, only when you declare a pointer to a structure does it's context become important.

In the case of a dll I would normally use a normal variable as opposed to a pointer. I'd probably name it something like 'ptrFunction' though.

I hope this helps?

**EDIT : beaten again! Too slow. :)
I may look like a mule, but I'm not a complete ass.
mubinaktan
User
User
Posts: 19
Joined: Mon Oct 08, 2007 11:25 am

Ah yes... C/C++ made the confusion

Post by mubinaktan »

In Purebasic, a pointer is just a variable of type long which we usually use to store addresses.
That makes things clearer. C/C++ made my mind confused...

My thanks go to all PB fans...
Post Reply