Differenze tra le versioni di "Memset ()"

Da Gambas-it.org - Wikipedia.
Riga 13: Riga 13:
 
  <FONT color=Gray>' ''void * memset(void *buffer, int c, size_t count)''
 
  <FONT color=Gray>' ''void * memset(void *buffer, int c, size_t count)''
 
  ' ''Copies the character c (an unsigned char) to the first count characters of the string pointed to, by the argument buffer.''</font>
 
  ' ''Copies the character c (an unsigned char) to the first count characters of the string pointed to, by the argument buffer.''</font>
  Private <FONT color=#B22222>Extern memset</font>(dest As integer[], c As Integer, count As Long) In "<FONT color=#B22222>libc:6</font>"
+
  Private <FONT color=#B22222>Extern memset</font>(dest As integer[], c As Byte, count As Long) In "<FONT color=#B22222>libc:6</font>"
 
   
 
   
 
  '''Public''' Sub Main()
 
  '''Public''' Sub Main()

Versione delle 13:17, 6 set 2015

La funzione della libreria di C

void * memset( void *buffer, int c, size_t count )

imposta tutti i valori di un vettore (array) a zero.


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

Private Extern memset(buffer As [Pointer], c As Byte, count As Long) In "libc:6"

Il primo valore potrà essere a seconda delle circostanze semplicemente un Puntatore, oppure un Vettore, o una Stringa.


Semplice esempio di uso in Gambas, nel quale si azzereranno i valori di un vbettore di tipo Integer[ ]:

' void * memset(void *buffer, int c, size_t count)
' Copies the character c (an unsigned char) to the first count characters of the string pointed to, by the argument buffer.
Private Extern memset(dest As integer[], c As Byte, count As Long) In "libc:6"

Public Sub Main()

 Dim cc As Integer[] = [1, 2, 3, 4, 5]
 Dim j As Byte
 

' Se il vettore è di tipo "Integer", allora il valore del 3° parametro della funzione
' va moltiplicato per 4, poiché il valore di tipo "Intero" occupa nella memoria 4 byte:
   memset(cc, 0, cc.Count * SizeOf(gb.Integer))
  
   For j = 0 To cc.Max
     Print cc[j]
   Next

End


In quest'altro esempio si modificheranno i primi 4 caratteri di una stringa:

' void * memset(void *buffer, int c, size_t count)
' Copies the character c (an unsigned char) to the first count characters of the string pointed to, by the argument buffer.
Private Extern memset(buffer As String, c As Byte, count As Long) In "libc:6"


Public Sub Main()

 Dim s As String

  s = "Testo qualsiasi"

  memset(s, Asc("a"), 4)

  Print s

End