Our full technical support staff does not monitor this forum. If you need assistance from a member of our staff, please submit your question from the Ask a Question page.


Log in or register to post/reply in the forum.

Pointers and Arrays


ryan42 Oct 19, 2016 03:16 AM

Hi,

I’m trying to pass the pointer of an array into a function and then access that arrays data from within using that pointer, see below?

Public tArray(5) = {1,2,3,4,5}
Public out, count

Function test(tPtr As Long)
  Dim i
  For i = 1 To 5 Step 1
    Delay(1,1500,mSec)
    count = i
    out = !tPtr(i)' Im expecting this to loop though and output 1,2,3,4,5 into out, I also tried !(tPtr+i)
  Next i
EndFunction

BeginProg
  Scan (10,Sec,0,0)
    test(@tArray)
  NextScan
EndPorg

Can anyone help me on using the pointers in this way? The help on pointers isn’t as detailed as some other help topics.


kirving Oct 19, 2016 06:33 PM

My only use of pointers so far has been to pass an array element to a function, but the function argument type needed to be tagged as a pointer for it to work. That may be particular to the string type, but maybe worth a try? I'd also maybe try eliminating 'as long' from the function declaration, as the docs say that long is the default pointer type.

Here's the function; note 'as string!' in the declaration:

  function cam_get_buffer(bufptr as string! * 1024, size as long) as long
    dim bytes as long '' bytes returned on each serialInBlock() call
    dim ofs as long '' offset into destination buffer
     ofs = 0
     do
       bytes = serialInBlock(handle, !bufptr, size-ofs)
       if bytes > 0 then ofs = ofs + bytes
     loop until ofs = size OR handle = 0
    movebytes(!bufptr, ofs, chr(&H00), 0, 1)
    return ofs
  endfunction

Called from the program as:

   do
    accum_i = accum_i + 1
    got_bytes = cam_get_buffer(@accum(accum_i), ACCUM_SIZE)
  loop while got_bytes > 0 AND accum_i <= ACCUM_ARRAY

Another thought is maybe you'd need to declare tPtr as an array in the function declaration. I agree that the documentation is pretty lean on the use of pointers.

    

Log in or register to post/reply in the forum.