Differenze tra le versioni di "Memset ()"

Da Gambas-it.org - Wikipedia.
Riga 14: Riga 14:
 
  <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 Byte[], c As Byte, count As Long) In "<FONT color=#B22222>libc:6</font>"
+
  Private Extern<FONT color=#B22222> memset</font>(dest As Byte[], c As Integer, count As Long) In "<FONT color=#B22222>libc:6</font>"
 
   
 
   
 
   
 
   
Riga 32: Riga 32:
 
  <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 Pointer, c As Byte, count As Long) In "<FONT color=#B22222>libc:6</font>"
+
  Private Extern<FONT color=#B22222> memset</font>(dest As Pointer, c As Integer, count As Long) In "<FONT color=#B22222>libc:6</font>"
 
   
 
   
 
   
 
   
Riga 53: Riga 53:
 
  <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 Byte, count As Long) In "<FONT color=#B22222>libc:6</font>"
+
  Private Extern<FONT color=#B22222> memset</font>(dest As integer[], c As Integer, count As Long) In "<FONT color=#B22222>libc:6</font>"
 
   
 
   
 
  '''Public''' Sub Main()
 
  '''Public''' Sub Main()
Riga 75: Riga 75:
 
  <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>(buffer As String, c As Byte, count As Long) In "<FONT color=#B22222>libc:6</font>"
+
  Private Extern<FONT color=#B22222> memset</font>(buffer As String, c As Integer, count As Long) In "<FONT color=#B22222>libc:6</font>"
 
   
 
   
 
   
 
   

Versione delle 10:06, 20 dic 2016

La funzione della libreria di C

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

copia il valore di "c" nell'area di memoria (Puntatore oppure vettore) per una quantità stabilita nell'argomento count.
V'è da precisare che il valore del secondo parametro viene passato come un intero (int), ma la funzione stessa riempie il blocco di memoria utilizzando la conversione "char senza-segno" (il tipo Byte di Gambas) di questo valore.


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 [vettore/puntatore], 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 assegna il valore 100 agli elementi di un vettore di tipo Byte[ ]:

' 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 Byte[], c As Integer, count As Long) In "libc:6"


Public Sub Main()

 Dim bb As New Byte[10]
 Dim b As Byte
 
  memset(bb, 100, 10)
   
  For Each b In bb
    Print b
  Next
  
End

Nello stesso esempio la destinazione potrà essere anche un Puntatore ad un vettore:

' 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 Pointer, c As Integer, count As Long) In "libc:6"


Public Sub Main()

 Dim bb As New Byte[10]
 Dim b As Byte
 
' In questo caso al primo argomento della funzione passiamo il Puntatore al vettore mediante la proprietà .Data di quest'ultimo:
  memset(bb.Data, 100, 10)
   
  For Each b In bb
    Print b
  Next
  
End


Altro esempio nel quale si azzereranno tutti gli elementi di un vettore 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 Integer, count As Long) In "libc:6"

Public Sub Main()

 Dim ii As Integer[] = [1, 2, 3, 4, 5]
 Dim i As Integer
 

' 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(ii, 0, ii.Count * SizeOf(gb.Integer))
  
   For Each i in ii
     Print i
   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 Integer, count As Long) In "libc:6"


Public Sub Main()

 Dim s As String

  s = "Testo qualsiasi"

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

  Print s

End