Syntax for accessing an element of a (pointer) array ?

Just starting out? Need help? Post your questions and find answers here.
MarkDM
New User
New User
Posts: 5
Joined: Fri Apr 18, 2025 7:45 am
Location: Flanders, Belgium

Syntax for accessing an element of a (pointer) array ?

Post 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 ?
User avatar
Bisonte
Addict
Addict
Posts: 1313
Joined: Tue Oct 09, 2007 2:15 am

Re: Syntax for accessing an element of a (pointer) array ?

Post 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
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
User avatar
STARGÅTE
Addict
Addict
Posts: 2235
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Syntax for accessing an element of a (pointer) array ?

Post 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
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
PBJim
Enthusiast
Enthusiast
Posts: 296
Joined: Fri Jan 19, 2024 11:56 pm

Re: Syntax for accessing an element of a (pointer) array ?

Post 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)
SMaag
Enthusiast
Enthusiast
Posts: 325
Joined: Sat Jan 14, 2023 6:55 pm
Location: Bavaria/Germany

Re: Syntax for accessing an element of a (pointer) array ?

Post 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
AZJIO
Addict
Addict
Posts: 2191
Joined: Sun May 14, 2017 1:48 am

Re: Syntax for accessing an element of a (pointer) array ?

Post 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)
MarkDM
New User
New User
Posts: 5
Joined: Fri Apr 18, 2025 7:45 am
Location: Flanders, Belgium

Re: Syntax for accessing an element of a (pointer) array ?

Post by MarkDM »

Thanks to all for the answers.
Regards,
Mark
Post Reply