Qbasicnews.com

Full Version: Help about pointers
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have never used pointers, so it's a newbie question..

I have in a library module
a sub
Code:
sub foo (ubyte ptr)
then i have an array of pointers (parameters to foo)
Code:
dim myarray(10) of ubyte ptr
and an init function that dims the array of ptrs and returns a pointer to it
Code:
function init() as ?????

My goal is to avoid declaring muarray as COMMON (this would be a QB solution)
and be able, in ANOTHER MODULE:

to call to init, retrieveing the pointer to myarray
Code:
dim p as ?????
p=init()

then call foo passing to it one of the elements of my array
Code:
foo(*p(i))
I can't find the correct set of type declarations to make it compile..
I don't exactly see what you're trying to do... but I have something to say =) First of all:

Code:
Private Sub Foo(theData As UByte Ptr)
Declares a sub that takes a pointer a parameter (note that pointer is NOT a datatype Smile)

Now,
Code:
Dim Shared myarray(10) As UByte Ptr
(note, shared because else init() can't access it)
I don't know what you want to do with this, but you make 11 references to memory that contain nothing. I presume you want the init() function to create memory for each of these 11 pointers? That's how I interpreted your text. Then this code is ok.
Then you have the function:
Code:
Private Function init As ULONG Ptr
   Dim theLoop As UInteger
   For theLoop = 0 To UBound(myarray)
      ' allocate 16 bytes for each pointer
      myarray(theLoop) = callocate(16)
   Next the Loop

   ' return the pointer
   init = @myarray(0)
   '(NOTE: @ is the Address-of operator, gives the pointer of a variable)
End Function
Note that a pointer to a pointer is a ulong ptr because a pointer is ulong.

Then in this other module you can do:
Code:
Dim p As ULONG Ptr
p = init()

And you can access the foo by
Code:
foo(p[i])

It works (tried) Big Grin

I tried it with this code (sorry uncommented):
Code:
#define ULONG unsigned long

Declare Function init () As ULONG Ptr
Declare Sub fillgradient(thememory As UByte Ptr)

Dim Shared myarray(10) As UByte Ptr

Dim p As ULONG Ptr
p = init

Dim otherpointer As UByte Ptr
For i = 0 To 10
   otherpointer = p[i]
   For j = 0 To 15
      Print otherpointer[j]; " ";
   Next j  
   Print
   deallocate myarray(i)
Next i

Sleep
End

Private Function init () As ULONG Ptr
   For i = 0 To 10
      myarray(i) = callocate(16)
      fillgradient(myarray(i))
   Next i  
   Dim retVal As ULONG Ptr
   retval = @myarray(0)
   init = retval
End Function

Private Sub fillgradient(thememory As UByte Ptr)
   For i = 0 To 15
      thememory[i] = i
   Next i  
End Sub
Ok, imagine i want to call fillgradient from the other module, not from init. I tried this ant it won't compile:

The library:
Code:
#include "neolib.bi"

Dim Shared myarray(10) As UByte Ptr

Private Function init () As ULONG Ptr
   For i = 0 To 10
      myarray(i) = callocate(16)
    Next i    
   Dim retVal As ULONG Ptr
   retval = @myarray(0)
   init = retval
End Function

Private Sub fillgradient(thememory As UByte Ptr)
   For i = 0 To 15
      thememory[i] = i
   Next i    
End Sub

The include:
Code:
#define ULONG unsigned long
Declare Function init () As ULONG Ptr
Declare Sub fillgradient(thememory As UByte Ptr)

and the main module
Code:
#include "neolib.bi"
Dim p As ULONG Ptr
p = init()
fillgradient(p[i])
This gives the error: Passing a scalar as a pointer in line (the call to fillgradient).

This is what I'm trying to do. Is it possible?






[/code]
Quote:
Code:
#include "neolib.bi"
Dim p As ULONG Ptr
p = init()
fillgradient(p[i])
This gives the error: Passing a scalar as a pointer in line (the call to fillgradient).

This is what I'm trying to do. Is it possible?
=)
It's because the p pointer refers to a ULONG. So if you do p[i] it returns a ULONG, while the fillgradient function requires a UByte Ptr.
In the code I posted above I dereferred this inconvenience by doing:
Code:
Dim otherpointer As UByte Ptr
otherpointer = p[i]
Then the otherpointer refers to the memory location stored in p[i].
Then you can do
Code:
fillgradient(otherpointer)

Now you're passing a pointer to the function, not a scalar (not-pointer). Smile
I hope I'm right though, and it works now Wink

Btw, don't forget to deallocate the memory allocated at the end of the program.

Big Grin
It works.
Thanks, Neo!

I nevet thought it was so easy. I was trying to declare a ubyte ptr ptr Big Grin
Big Grin
No problem at all Antoni =)

And as I said before, a pointer to a pointer to something is to be declared as ULONG Ptr =) Because, infact a pointer is nothing else but a ULONG, which holds a memory location at which something is located.

Hope I was of any use Smile

Neo