Page 1 of 1
Syntax for accessing an element of a (pointer) array ?
Posted: Mon Apr 21, 2025 7:18 am
by MarkDM
I have this:
Code: Select all
Define *Buffer
*Buffer = AllocateMemory(100)
....
Result = ReadSerialPortData(1, *Buffer, Len)
I want to search for char 13, carriage return.
Problem: syntax for testing value of elements in the array.
Code: Select all
While (*Buffer[Index] <> 13)
Index = Index + 1
...
Is it possible to access the elements of the array *buffer individually?
What is the syntax in PureBasic ?
Is there an increment operator ?
Re: Syntax for accessing an element of a (pointer) array ?
Posted: Mon Apr 21, 2025 7:29 am
by Bisonte
Your *Buffer is not an Array.
*Buffer is simply a pointer to a memory adress. not more. Searching is like :
Code: Select all
Define *Buffer
*Buffer = AllocateMemory(100)
For i = 0 to 99
If *Buffer + i = 13
Debug "Hit"
Break
Endif
Next i
Re: Syntax for accessing an element of a (pointer) array ?
Posted: Mon Apr 21, 2025 7:56 am
by STARGÅTE
You can use a structure with a "0-Size-Array ":
Code: Select all
Structure CharacterArray
c.c[0]
EndStructure
Define *Buffer.CharacterArray = AllocateMemory(100)
PokeS(*Buffer, "Hello"+#CR$)
For Index = 0 To 49 ; One character has 2 bytes!
If *Buffer\c[Index] = #CR
Debug "CR at " + Index
EndIf
Next
Re: Syntax for accessing an element of a (pointer) array ?
Posted: Mon Apr 21, 2025 8:48 am
by PBJim
STARGÅTE's example is very close to the C use of a character array.
Incidentally, the first reply showing If *Buffer + i = 13 doesn't take into account the buffer as a character structure that is necessary in order to reference the byte values. Therefore it is looking for memory location of 13.
I suppose ordinarily, in PureBasic, assuming speed is important, we would tend to reference the bytes as CHARACTER type in a loop. The search character is parameterised in the below, as a function. Given that you're reading serial interface data, I'm not certain, but it might be that your data is ASCII, in which case the below would need to be changed — the character length therefore 1, the type ASCII and the structure element \a instead of \c
Code: Select all
EnableExplicit
; **
; ** Returns buffer position (0 to length - 1) of specified character
; ** -1 signifies not found
; **
Procedure Locate_Char(*StrPtr.Character, length.i, character.i)
Protected index.i ; Byte offset, commences 0
While index.i < length
If *StrPtr\c = character.i ; Character no. specified
ProcedureReturn index.i ; Return offset
EndIf
*StrPtr + SizeOf(character) ; Advance byte pos. by character size
index.i + 2 ; ... or just 2
Wend
ProcedureReturn -1 ; Not found
EndProcedure
; **
; ** Main
; **
Define *Buffer
*Buffer = AllocateMemory(100, #PB_Memory_NoClear)
; Result = ReadSerialPortData(1, *Buffer, Len)
Debug Locate_Char(*Buffer, 100, 0) ; 100 bytes, search for char 0
ShowMemoryViewer(*buffer, 100)
Re: Syntax for accessing an element of a (pointer) array ?
Posted: Mon Apr 21, 2025 1:44 pm
by SMaag
@MarcDM
As new PB user, take a look at the PB-Extention Module. There I show often needed things, not directly integrated in PB.
There you can find how to use extended Structure/Pointer use for Character based operations, or universal Pointer operations.
Search for the Structure pChar, pANY
In the Modul Testcode section, you can find how to use pChar
https://github.com/Maagic7/PureBasicFra ... dule_PB.pb
Re: Syntax for accessing an element of a (pointer) array ?
Posted: Mon Apr 21, 2025 5:13 pm
by AZJIO
character-by-character parsing of a string
https://www.purebasic.fr/english/viewtopic.php?t=86726
Code: Select all
Define *m = AllocateMemory(100)
Define *Buffer.Character = *m
PokeS(*Buffer, "Hello" + #CR$)
While *Buffer\c
If *Buffer\c = #CR
Debug "CR found"
Break
EndIf
*Buffer + 2
Wend
FreeMemory(*m)
Re: Syntax for accessing an element of a (pointer) array ?
Posted: Wed Apr 23, 2025 5:49 pm
by MarkDM
Thanks to all for the answers.
Regards,
Mark