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 Byte, count As Long) In "<FONT color=#B22222>libc:6</font>"
+
  Private <FONT color=#B22222>Extern memset</font>(dest As Byte[], c As Byte, count As Long) In "<FONT color=#B22222>libc:6</font>"
 
   
 
   
 
   
 
   
Riga 22: Riga 22:
 
    
 
    
 
   <FONT color=#B22222>memset</font>(bb, 100, 10)
 
   <FONT color=#B22222>memset</font>(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:
 +
<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>
 +
Private <FONT color=#B22222>Extern memset</font>(dest As Pointer, c As Byte, count As Long) In "<FONT color=#B22222>libc:6</font>"
 +
 +
 +
'''Public''' Sub Main()
 +
 +
  Dim bb As New Byte[10]
 +
  Dim b As Byte
 +
 
 +
<FONT color=Gray>' ''In questo caso al primo argomento della funzione passiamo il ''Puntatore'' al vettore mediante la proprietà ''.Data'' di quest'ultimo:''</font>
 +
  memset(bb<FONT color=#B22222>.Data</font>, 100, 10)
 
      
 
      
 
   For Each b In bb
 
   For Each b In bb

Versione delle 16:01, 13 ott 2015

La funzione della libreria di C

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

copia il valore di "c" di tipo Byte nell'area di memoria (Puntatore oppure vettore) per una quantità stabilita nell'argomento count.


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 Byte, 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 Byte, 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 Byte, 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 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