Strchr ()

Da Gambas-it.org - Wikipedia.

La funzione strchr(), dichiarata nel file header "/usr/include/string.h" come segue:

char *strchr (char *__s, int __c)

cerca la prima occorrenza del valore __c nella stringa __s.

Volendola utilizzare in Gambas, bisognerà dichiararla con Extern, nonché dichiarare la libreria di C: libc.so.6, nella quale la funzione è contenuta:

Private Extern strchr(__s As String, __c As Integer) As Pointer In "libc:6"


Mostriamo un esempio, nel quale si sostituirà un carattere all'interno di una stringa. Bisognerà utilizzare i " Memory Stream " per scrivere nell'area di memoria puntata dal Puntatore restituito dalla funzione esterna strchr( ):

Library "libc:6"

' char *strchr (char *__s, int __c)
' Find the first occurrence of C in S.
Private Extern strchr(__s As String, __c As Integer) As Pointer


Public Sub Main()
 
 Dim s As String = "abcdefg"
 Dim st As Stream
  
  st = Memory strchr(s, Asc("d")) For Write
  Write #st, CByte(Asc("X")) As Byte
  st.Close
  
  Print s
  
End



Riferimenti